这次我们讲RadioButton和CheckBox控件,首先我们讲RadioButton控件。
相信大家一定看过西游记,里面有妖精抓唐僧的场景,我们就用这两个控件来模拟一下,RadionButton控件呢是说每次妖精只能抓一个人,每次一个,抓几个就得抓几次,这可把妖精们忙坏了,呵呵
我们看一下main.xml中的代码呢:
<?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"> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="唐僧" android:id="@+id/tangseng"/> <RadioButton android:id="@+id/wukong" android:text="孙悟空" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="猪八戒" android:id="@+id/bajie"/> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="沙和尚" android:id="@+id/shaseng"/> </RadioGroup> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮" android:id="@+id/button"></Button> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello" android:id="@+id/text"></TextView> </LinearLayout>
Activity中的java代码如下:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.TextView; public class ButtonDemoActivity extends Activity implements OnClickListener { private TextView text = null; private RadioButton tangseng; private RadioButton wukong; private RadioButton bajie; private RadioButton shaseng; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 通过ID查找到main.xml中的TextView控件 text = (TextView) findViewById(R.id.text); //唐僧单选框 tangseng = (RadioButton)findViewById(R.id.tangseng); //悟空单选框 wukong = (RadioButton)findViewById(R.id.wukong); //八戒单选框 bajie = (RadioButton)findViewById(R.id.bajie); //沙僧单选框 shaseng = (RadioButton)findViewById(R.id.shaseng); // 通过ID查找到main.xml中的Button控件 Button button = (Button) findViewById(R.id.button); // 为Button控件增加单击监听器 button.setOnClickListener(this); } private void updateText(String string) { // 将文本信息设置给TextView控件显示出来 text.setText(string); } @Override public void onClick(View v) { String str = ""; //唐僧单选框被选中 if(tangseng.isChecked()){ str += "唐僧~"; } //悟空单选框被选中 if(wukong.isChecked()){ str += "悟空~"; } //八戒单选框被选中 if(bajie.isChecked()){ str += "八戒~"; } //沙僧单选框被选中 if(shaseng.isChecked()){ str += "沙僧~"; } //没有人被选中 if(str.equals("")){ str += "没有人"; } str +="被妖精抓走了!"; updateText(str); } }
每次只抓一个多麻烦啊,有妖精想了,我本领高强,我一次多抓几个,这样省事多了,这下,我们就用到了CheckBox控件啦。
main.xml代码如下:
<?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"> <CheckBox android:text="唐僧" android:id="@+id/tangseng" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> <CheckBox android:text="孙悟空" android:id="@+id/wukong" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> <CheckBox android:text="猪八戒" android:id="@+id/bajie" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> <CheckBox android:text="沙和尚" android:id="@+id/shaseng" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮" android:id="@+id/button"></Button> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello" android:id="@+id/text"></TextView> </LinearLayout>
Activity中的java代码如下:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.TextView; public class ButtonDemoActivity extends Activity implements OnClickListener { private TextView text = null; private CheckBox tangseng; private CheckBox wukong; private CheckBox bajie; private CheckBox shaseng; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 通过ID查找到main.xml中的TextView控件 text = (TextView) findViewById(R.id.text); //唐僧复选框 tangseng = (CheckBox)findViewById(R.id.tangseng); //悟空复选框 wukong = (CheckBox)findViewById(R.id.wukong); //八戒复选框 bajie = (CheckBox)findViewById(R.id.bajie); //沙僧复选框 shaseng = (CheckBox)findViewById(R.id.shaseng); // 通过ID查找到main.xml中的Button控件 Button button = (Button) findViewById(R.id.button); // 为Button控件增加单击监听器 button.setOnClickListener(this); } private void updateText(String string) { // 将文本信息设置给TextView控件显示出来 text.setText(string); } @Override public void onClick(View v) { String str = ""; //唐僧复选框被选中 if(tangseng.isChecked()){ str += "唐僧~"; } //悟空复选框被选中 if(wukong.isChecked()){ str += "悟空~"; } //八戒复选框被选中 if(bajie.isChecked()){ str += "八戒~"; } //沙僧复选框被选中 if(shaseng.isChecked()){ str += "沙僧~"; } //没有人被选中 if(str.equals("")){ str += "没有人"; } str +="被妖精抓走了!"; updateText(str); } }
哈哈,这下妖精们高兴了,一次把四个都抓走都行了?什么?四个都抓走了,那谁救唐僧啊?放心,你看西游记最后唐僧不也是没被妖精吃掉嘛,高人自有人相救,呵呵,啰嗦了,这一篇就到这里吧。