ANDROID_MARS 학습노트S01 원본005_RadioGroup\CheckBox\Toast

13412 단어
코드
1.xml(1)radio.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <TextView
 8     android:id="@+id/textView1"  
 9     android:layout_width="fill_parent" 
10     android:layout_height="wrap_content" 
11     android:text="@string/hello"
12     />
13 <RadioGroup
14     android:id="@+id/genderGroup"
15     android:layout_width="wrap_content" 
16     android:layout_height="wrap_content" 
17     android:orientation="vertical"
18     >
19     <RadioButton
20         android:id="@+id/femaleButton"
21          android:layout_width="wrap_content" 
22           android:layout_height="wrap_content" 
23           android:text="@string/female"
24           />
25     <RadioButton
26         android:id="@+id/maleButton"
27          android:layout_width="wrap_content" 
28           android:layout_height="wrap_content" 
29           android:text="@string/male"
30           />
31 </RadioGroup>
32 <CheckBox
33     android:id="@+id/swim"
34      android:layout_width="wrap_content" 
35       android:layout_height="wrap_content" 
36       android:text="@string/swim"
37       />
38 <CheckBox
39     android:id="@+id/run"
40      android:layout_width="wrap_content" 
41       android:layout_height="wrap_content" 
42       android:text="@string/run"
43       />
44 <CheckBox
45     android:id="@+id/read"
46      android:layout_width="wrap_content" 
47       android:layout_height="wrap_content" 
48       android:text="@string/read"
49       />
50 </LinearLayout>

 
2.java(1)RadioTest.java
 1 package mars.activity07;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.CheckBox;
 6 import android.widget.CompoundButton;
 7 import android.widget.RadioButton;
 8 import android.widget.RadioGroup;
 9 import android.widget.Toast;
10 
11 public class RadioTest extends Activity {
12     /** Called when the activity is first created. */
13     //         
14     private RadioGroup genderGroup = null;
15     private RadioButton femaleButton = null;
16     private RadioButton maleButton = null;
17     private CheckBox swimBox = null;
18     private CheckBox runBox = null;
19     private CheckBox readBox = null;
20     @Override
21     public void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.radio);
24         //     ID          
25         genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
26         femaleButton = (RadioButton)findViewById(R.id.femaleButton);
27         maleButton = (RadioButton)findViewById(R.id.maleButton);
28         swimBox = (CheckBox)findViewById(R.id.swim);
29         runBox = (CheckBox)findViewById(R.id.run);
30         readBox = (CheckBox)findViewById(R.id.read);
31         // RadioGroup     ,      ,       Button          
32         genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
33             
34             @Override
35             public void onCheckedChanged(RadioGroup group, int checkedId) {
36                 // TODO Auto-generated method stub
37                 if(femaleButton.getId() == checkedId){
38                     System.out.println("famale");
39                     Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();
40                 }
41                 else if(maleButton.getId() == checkedId)
42                 {
43                     System.out.println("male");
44                 }
45             }
46         });
47         
48         //          
49         swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
50             
51             @Override
52             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
53                 // TODO Auto-generated method stub
54                 if(isChecked)
55                 {
56                     System.out.println("swim is checked");
57                 }
58                 else
59                 {
60                     System.out.println("swim is unchecked");
61                 }
62             }
63         });
64         runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
65             
66             @Override
67             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
68                 // TODO Auto-generated method stub
69                 if(isChecked)
70                 {
71                     System.out.println("run is checked");
72                 }
73                 else
74                 {
75                     System.out.println("run is unchecked");
76                 }
77             }
78         });
79         readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
80             
81             @Override
82             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
83                 // TODO Auto-generated method stub
84                 if(isChecked)
85                 {
86                     System.out.println("read is checked");
87                 }
88                 else
89                 {
90                     System.out.println("read is unchecked");
91                 }
92             }
93         });
94     }
95     
96 }

 
공식 문서 설정 감청은 xml에서android:onClick="onCheckboxClicked"/>를 설정하고 자바 파일에서 onCheckboxClicked 방법을 작성하여 switch로 선택 상태를 판단합니다
 
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent">
 6     <CheckBox android:id="@+id/checkbox_meat"
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:text="@string/meat"
10         android:onClick="onCheckboxClicked"/>
11     <CheckBox android:id="@+id/checkbox_cheese"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:text="@string/cheese"
15         android:onClick="onCheckboxClicked"/>
16 </LinearLayout>

 
 
 1 public void onCheckboxClicked(View view) {
 2     // Is the view now checked?
 3     boolean checked = ((CheckBox) view).isChecked();
 4     
 5     // Check which checkbox was clicked
 6     switch(view.getId()) {
 7         case R.id.checkbox_meat:
 8             if (checked)
 9                 // Put some meat on the sandwich
10             else
11                 // Remove the meat
12             break;
13         case R.id.checkbox_cheese:
14             if (checked)
15                 // Cheese me
16             else
17                 // I'm lactose intolerant
18             break;
19         // TODO: Veggie sandwich
20     }
21 }

좋은 웹페이지 즐겨찾기