android基本控件RadioButton/CheckBox

来源:互联网 发布:清理后台的软件 编辑:程序博客网 时间:2024/06/01 09:05

MainActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <RadioGroup        android:id="@+id/radioGroup"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:orientation="vertical" >        <RadioButton            android:id="@+id/female"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/female" >        </RadioButton>        <RadioButton            android:id="@+id/male"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/male" >        </RadioButton>    </RadioGroup>    <CheckBox        android:id="@+id/swim"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/swim" />    <CheckBox        android:id="@+id/run"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/run" />    <CheckBox        android:id="@+id/write"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/write" /></LinearLayout>


MainActivity.java



import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity {private  RadioGroup radioGroup;private  RadioButton female;private RadioButton male;private CheckBox run,swim,write;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);radioGroup=(RadioGroup)findViewById(R.id.radioGroup);female=(RadioButton)findViewById(R.id.female);male=(RadioButton)findViewById(R.id.male);run=(CheckBox)findViewById(R.id.run);swim=(CheckBox)findViewById(R.id.swim);write=(CheckBox)findViewById(R.id.write);/*监听器*/radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(female.getId() == checkedId){System.out.println("女生");Toast.makeText(MainActivity.this, "女生", Toast.LENGTH_LONG).show(); }else if(male.getId()==checkedId){System.out.println("男生");Toast.makeText(MainActivity.this, "男生", Toast.LENGTH_LONG).show(); }} });/*给每个checkBox都要加监听器*/run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif(isChecked){Toast.makeText(MainActivity.this, "run", Toast.LENGTH_LONG).show();}}});write.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif(isChecked){Toast.makeText(MainActivity.this, "write", Toast.LENGTH_LONG).show();}}});swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif(isChecked){Toast.makeText(MainActivity.this, "swim", Toast.LENGTH_LONG).show();}}});}}



string.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Activity_04</string>    <string name="hello_world">单选和多选</string>    <string name="female">female</string>    <string name="male">male</string>      <string name="swim">swim</string>        <string name="run">run</string>          <string name="write">write</string></resources>




原创粉丝点击