반응형
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 |
package com.ktds.myradiobutton;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private RadioButton rdoStud;
private RadioButton rdoTeac;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView가 xml 파일을 불러옴
setContentView(R.layout.activity_main);
rdoStud = (RadioButton) findViewById(R.id.rdoStud);
rdoTeac = (RadioButton) findViewById(R.id.rdoTeac);
rdoStud.setOnCheckedChangeListener(new MyRadioButtonListener());
rdoTeac.setOnCheckedChangeListener(new MyRadioButtonListener());
}
private class MyRadioButtonListener implements RadioButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
String message = String.format("%s 선택됨",buttonView.getText());
/* text를 보기위한 방법이 여러가지
1. getApplicationContext() : 현재 실행되고 있는 context(activity)
2. buttonView.getContext() : 현재 실행되고 있는 application의 context를 가져옴
*/
Toast.makeText(buttonView.getContext(), message, 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 |
<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/textView"
android:layout_gravity="center_horizontal"
android:autoText="false" />
<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/rdoStud" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="선생"
android:id="@+id/rdoTeac"/>
</RadioGroup>
</LinearLayout>
|
cs |
반응형
'IT > Android' 카테고리의 다른 글
Widget - check box (0) | 2015.06.16 |
---|---|
실습 (0) | 2015.06.16 |
Android 프로그래밍 (0) | 2015.06.15 |
Android Project 만들기 (0) | 2015.06.15 |
ANDROID STUDIO 설치 및 설정 (0) | 2015.06.15 |