본문 바로가기
category/android studio 앱 개발

android studio 닉네임 생성기

by 자운대고라니 2021. 4. 28.
반응형

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.gwdevelop.randomnick;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
 
 
public class MainActivity extends AppCompatActivity {
 
    private TextView result;
    private TextView number;
    private TextView number_result;
    private Button btn_mybutton;
 
    private String pop_result = "2";
    private String total_result = "";
 
    private AdView mAdView;
    private String log = "LOG";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
 
        MobileAds.initialize(thisnew OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } });
 
        mAdView = findViewById(R.id.banner);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
 
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
                Log.d(log, "finishes loading.");
            }
 
            @Override
            public void onAdFailedToLoad(LoadAdError adError) {
                // Code to be executed when an ad request fails.
                Log.d(log, "request fails.");
            }
 
            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
                Log.d(log, "opens an overlay that, covers the screen.");
            }
 
            @Override
            public void onAdClicked() {
                // Code to be executed when the user clicks on an ad.
                Log.d(log, "user clicks on an ad.");
            }
 
            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
                Log.d(log, "the user has left the app.");
            }
 
            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
                Log.d(log, "user is about to return to the app after tapping on an ad.");
            }
        });
 
        init();
    }
    
        private void init() {
        result = findViewById(R.id.result);
        number = findViewById(R.id.number);
        number_result = findViewById(R.id.number_result);
        btn_mybutton = findViewById(R.id.btn_mybutton);
        number_result.setText("2글자");
    }
 
    //popup 클릭 이벤트
    public void popupClick(View v) {
        Intent intent = new Intent(this, popupActivity.class);
        //intent.putExtra("data", "Test Popup");
        startActivityForResult(intent, 1);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                //데이터 받기
                pop_result = data.getStringExtra("result");
                number_result.setText(pop_result + "글자");
            }
        }
 
    }
 
     public void btn_click(View v) {
        String a = "";
        String[] random_result = new String[]{'모든 글자'};
 
        for(int i=0;i<Integer.valueOf(pop_result);i++) {
            int randomNum = (int) (Math.random() * random_result.length);
            total_result+=random_result[randomNum];
        }
 
        result.setText(total_result);
        total_result = "";
    }
}
cs

popupActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.gwdevelop.randomnick;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.TextView;
 
public class popupActivity extends Activity {
 
    TextView txtText;
    private int radio_result = 2;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.popup_activity);
        
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.RadioGroup);
 
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String result;
                if(checkedId == R.id.two) radio_result=2;
                else if(checkedId == R.id.three) radio_result=3;
                else if(checkedId == R.id.four) radio_result=4;
                else if(checkedId == R.id.five) radio_result=5;
                else if(checkedId == R.id.six) radio_result=6;
            }
        });
    }
 
    //확인 버튼 클릭
    public void mOnClose(View v){
        //데이터 전달하기
 
        Intent intent = new Intent();
        intent.putExtra("result", Integer.toString(radio_result)); //결과 반환
        setResult(RESULT_OK, intent);
 
        //액티비티(팝업) 닫기
        finish();
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //바깥레이어 클릭시 안닫히게
        if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
            return false;
        }
        return true;
    }
 
    @Override
    public void onBackPressed() {
        //안드로이드 백버튼 막기
        return;
    }
}
cs

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_mybutton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="5dip"
 
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
 
        android:backgroundTint="@color/white"
        android:text="랜덤 닉네임 생성"
        android:textColor="@color/black"
        android:onClick="btn_click"/>
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_mybutton"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp"
        android:text="랜덤 닉네임"
        android:textColor="@color/black"
        android:id="@+id/result"
        android:textSize="30sp"
        android:textStyle="bold" />
 
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_above="@+id/result"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="글자 수 선택하기"
        android:layout_marginBottom="40dp"
        android:textStyle="bold"
        android:textColor="@color/black"
        android:id="@+id/number"
        android:onClick="popupClick"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/result"
        android:layout_centerHorizontal="true"
        android:id="@+id/number_result"
        android:layout_marginBottom="10dp"
        />
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-광고ID">
    </com.google.android.gms.ads.AdView>
 
</RelativeLayout>
 
 
cs

popup_activity.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_mybutton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="5dip"
 
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
 
        android:backgroundTint="@color/white"
        android:text="랜덤 닉네임 생성"
        android:textColor="@color/black"
        android:onClick="btn_click"/>
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_mybutton"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp"
        android:text="랜덤 닉네임"
        android:textColor="@color/black"
        android:id="@+id/result"
        android:textSize="30sp"
        android:textStyle="bold" />
 
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_above="@+id/result"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="글자 수 선택하기"
        android:layout_marginBottom="40dp"
        android:textStyle="bold"
        android:textColor="@color/black"
        android:id="@+id/number"
        android:onClick="popupClick"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/result"
        android:layout_centerHorizontal="true"
        android:id="@+id/number_result"
        android:layout_marginBottom="10dp"
        />
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-광고ID">
    </com.google.android.gms.ads.AdView>
 
</RelativeLayout>
 
 
cs

 

실행결과


개선할 부분 : 오류는 없음, 디자인 부분 

반응형

댓글