android中的单选和多选按钮的使用

来源:互联网 发布:淘宝主图素材怎么制作 编辑:程序博客网 时间:2024/05/29 13:06

1.布局文件:

<RadioGroup android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical"    android:id="@+id/radioGroup">    <RadioButton android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/radio1"        android:text="@string/female"/>    <RadioButton android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/radio2"        android:text="@string/male"/></RadioGroup><CheckBox android:id="@+id/singBox"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/sing"/><CheckBox android:id="@+id/runBox"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/run"/><CheckBox android:id="@+id/danceBox"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/dance"/>

2.activity

package com.example.android1;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.LinearLayout;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class LinearLayOut extends Activity{private RadioGroup radioGroup;private RadioButton radio1,radio2;private CheckBox runBox,singBox,danceBox;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button=(Button)findViewById(R.id.button);button.setOnClickListener(new MyButtonListener());//绑定监听器radio1=(RadioButton)findViewById(R.id.radio1);radio2=(RadioButton)findViewById(R.id.radio2);radioGroup=(RadioGroup)findViewById(R.id.radioGroup);runBox=(CheckBox)findViewById(R.id.runBox);singBox=(CheckBox)findViewById(R.id.singBox);danceBox=(CheckBox)findViewById(R.id.danceBox);//单选按钮监听器radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId){// TODO Auto-generated method stubif(radio1.getId()==checkedId){System.out.println("女");//Toast.makeText(LinearLayout.class, "选择了女", Toast.LENGTH_SHORT).show();Toast.makeText(LinearLayOut.this, "选择了女", Toast.LENGTH_SHORT).show();}else if(radio2.getId()==checkedId){System.out.println("男");}}});//复选框监听器,每一个checkbox都需要一个runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked){if(isChecked){System.out.println("runBox is selected");}else{System.out.println("runBox is unselected");}}});singBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked){if(isChecked){System.out.println("singBox is selected");}else{System.out.println("singBox is unselected");}}});danceBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked){if(isChecked){System.out.println("danceBox is selected");}else{System.out.println("danceBox is unselected");}}});}class MyButtonListener implements OnClickListener{@Overridepublic void onClick(View v){Intent intent=new Intent();intent.setClass(LinearLayOut.this, TableLayout.class);LinearLayOut.this.startActivity(intent);}}}


原创粉丝点击