Checkbox和RadioRadioButton及其实例

学习目标:掌握Checkbox和RadioRadioButton;掌握OnCheckedChangeListener的使用。

一:CheckBox的使用

   一:定义及特性

      1:CheckBox是选择框,只有选中和未选中两种状态。

      2:一半使用在多个选项都可以选择的情况下,例如:选择你感兴趣的话题。

      3:OnCheckedChangeListener是CompoundButton下的监听对象,因为CheckBox是CompoundButton的子类,所以OnCheckedChangeListener可以直接使用。

   二:例子

      

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="请选择你的爱好" />
    <CheckBox
        android:id="@+id/playball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="玩球"
    />
    <CheckBox
        android:id="@+id/readnovel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读小说"
    />
    <CheckBox
        android:id="@+id/playgame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打游戏"
    />
    <CheckBox
        android:id="@+id/readbooks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读书"
    />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查看选中" />

</LinearLayout>

private Button check;
    private CheckBox playball,readnovel,readbooks,playgame;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	    check=(Button)findViewById(R.id.button1);
	    playball=(CheckBox)findViewById(R.id.playball);
	    readnovel=(CheckBox)findViewById(R.id.readnovel);
	    readbooks=(CheckBox)findViewById(R.id.readbooks);
	    playgame=(CheckBox)findViewById(R.id.playgame);
	    //使用匿名内部类进行监听
	    check.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				shoecheck();
			}

			private void shoecheck() {
				// TODO 自动生成的方法存根
				String str = "";
				if(playball.isChecked())
				{
					str+=playball.getText().toString();
				}
				if(readnovel.isChecked())
				{
					str+=readnovel.getText().toString();
				}
				if(readbooks.isChecked())
				{
					str+=readbooks.getText().toString();
				}
				if(playgame.isChecked())
				{
					str+=playgame.getText().toString();
				}
			}
			
		});
	}
   
结果



     

猜你喜欢

转载自blog.csdn.net/xd15010130025/article/details/77803904
今日推荐