IT/Android
실습
바바옄
2015. 6. 16. 10:32
반응형
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 |
package com.ktds.myfirsttest;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private EditText etAge, etName;
private RadioButton rdoWoman;
private RadioButton rdoMan;
private RadioButton rdoStu;
private RadioButton rdoWorker;
private Button btnResult;
String gender, job;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
etAge = (EditText) findViewById(R.id.etAge);
rdoWoman = (RadioButton) findViewById(R.id.rdoWoman);
rdoMan = (RadioButton) findViewById(R.id.rdoMan);
rdoStu = (RadioButton) findViewById(R.id.rdoStu);
rdoWorker = (RadioButton) findViewById(R.id.rdoWorker);
btnResult = (Button) findViewById(R.id.btnResult);
rdoWoman.setOnCheckedChangeListener(new MyRadioButtonListener());
rdoMan.setOnCheckedChangeListener(new MyRadioButtonListener());
rdoStu.setOnCheckedChangeListener(new MyRadioButtonListener2());
rdoWorker.setOnCheckedChangeListener(new MyRadioButtonListener2());
btnResult.setOnClickListener(new ShowResultListener());
}
private class MyRadioButtonListener implements RadioButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
gender = buttonView.getText().toString();
}
}
}
private class MyRadioButtonListener2 implements RadioButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
job = buttonView.getText().toString();
}
}
}
private class ShowResultListener implements Button.OnClickListener{
@Override
public void onClick(View v) {
String result = String.format("이름은 %s 나이는 %s 성별은 %s 직업은 %s 입니다.",etName.getText().toString(),etAge.getText().toString(),job,gender);
Toast.makeText(v.getContext(),result,Toast.LENGTH_LONG).show();
}
}
}
|
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 |
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="이름"
android:id="@+id/tvName" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etName"
android:hint="이름을 입력하세요" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="나이"
android:id="@+id/tvAge" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etAge"
android:hint="나이를 입력하세요" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="성별"
android:id="@+id/tvGender" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="여자"
android:id="@+id/rdoWoman" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="남자"
android:id="@+id/rdoMan" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="직업"
android:id="@+id/tvJob" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="학생"
android:id="@+id/rdoStu" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="직장인"
android:id="@+id/rdoWorker" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="결과보기"
android:id="@+id/btnResult" />
</LinearLayout>
|
cs |
반응형