android控件:RadioGroup、RadioButton、CheckBox和Toast的使用
主要内容有:
1.RadioGroup和RadioButton的使用
所谓的RadioButton就是单选按钮,在一组单选按钮当中,只有一个能够选中;
2.CheckBox的使用
CheckBox是所选按钮,可以让用户一次性选中多个选项;
3.Toast的使用
Toast是一种弹出式的提示框,显示的时间可长可短。使用Toast可以很方便的显示一些提示用户的信息;
视频参见:
《android视频教程》第一季 第12集 Android常见控件(二)
代码分享:
1、页面布局文件radio.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 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <RadioGroup android:id="@+id/genderGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/femaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> <RadioButton android:id="@+id/maleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /> </RadioGroup> <CheckBox android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read" /> <CheckBox android:id="@+id/speak" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/speak" /> <CheckBox android:id="@+id/listen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/listen" /> </LinearLayout> |
2、字符串资源文件strings.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, myControl!</string> <string name="app_name">myControl</string> <string name="female">女</string> <string name="male">男</string> <string name="big">big</string> <string name="small">small</string> <string name="listen">听</string> <string name="speak">说</string> <string name="read">读</string> </resources> |
3、程序主文件myControl.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 | package com.cxybase.myControl; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class myControl extends Activity { /** Called when the activity is first created. */ private final static String MYTAG="cxybase"; //对控件对象进行声明 private RadioGroup genderGroup = null; private RadioButton maleButton = null; private RadioButton femaleButton = null; private CheckBox readBox = null; private CheckBox listenBox = null; private CheckBox speakBox = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio); //通过控件的ID来获取控件的对象 genderGroup = (RadioGroup)findViewById(R.id.genderGroup); maleButton = (RadioButton)findViewById(R.id.maleButton); femaleButton = (RadioButton)findViewById(R.id.femaleButton); readBox = (CheckBox)findViewById(R.id.read); listenBox = (CheckBox)findViewById(R.id.listen); speakBox = (CheckBox)findViewById(R.id.speak); //为RadioGroup设置监听器 genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(femaleButton.getId() == checkedId) { // System.out.println("female"); Log.i(MYTAG, "选中了female"); Toast.makeText(myControl.this, "选中了female", Toast.LENGTH_SHORT).show(); } else if(maleButton.getId() == checkedId) { // System.out.println("male"); Log.i(MYTAG, "male"); Toast.makeText(myControl.this, "选中了male", Toast.LENGTH_SHORT).show(); } } }); //为每一个CheckBox设置监听器 readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { // System.out.println("read is checked"); Log.i(MYTAG, "read is checked"); } else { // System.out.println("read is not checked"); Log.i(MYTAG, "read is no checked"); } } }); speakBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { // System.out.println("speak is checked"); Log.i(MYTAG, "speak is checked"); } else { Log.i(MYTAG, "speak is not checked"); // System.out.println("speak is not checked"); } } }); listenBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { // System.out.println("listen is checked"); Log.i(MYTAG, "listen is checked"); } else { // System.out.println("listen is not checked"); Log.i(MYTAG, "listen is not checked"); } } }); } } |
转载请标明出处:萝卜根
原文地址请标明:原文地址