android-----相对布局+单选按钮+多选按钮

来源:互联网 发布:乐乎公寓的房子怎么样 编辑:程序博客网 时间:2024/05/17 22:41

1、main.xml代码:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:padding="10px" >      <!--第一部分-->  <TextView        android:id="@+id/myTextView"        android:layout_width="fill_parent"        android:layout_height="wrap_content"android:text="Type here:"android:textSize="10pt"       /><EditText        android:id="@+id/myedit"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/myTextView"    />  <Button         android:id="@+id/ok"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="OK"        android:layout_below="@id/myedit"        android:layout_alignParentRight="true"    android:layout_marginLeft="10px"    />    <Button         android:id="@+id/cancel"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Cancel"        android:layout_toLeftOf="@id/ok"        android:layout_below="@id/myedit"    />    <!--第二部分-->    <RadioGroup         android:id="@+id/group"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:layout_below="@id/cancel">        <RadioButton           android:id="@+id/button01"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="@string/famle"            />        <RadioButton           android:id="@+id/button02"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="@string/male"/>      </RadioGroup>        <CheckBox            android:id="@+id/checkbox01"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/run"            android:layout_below="@id/group"/>                 <CheckBox            android:id="@+id/checkbox02"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/swim"           android:layout_below="@id/group"           android:layout_toRightOf="@id/checkbox01"/>                <CheckBox            android:id="@+id/checkbox03"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/read"           android:layout_below="@id/group"           android:layout_toRightOf="@id/checkbox02"/></RelativeLayout>

2、activity01代码:

package mars.activity01;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;import android.app.Activity;public class Activity01 extends Activity {private RadioGroup group=null;private RadioButton button01=null;private RadioButton button02=null;private CheckBox checkbox01=null;private CheckBox checkbox02=null;private CheckBox checkbox03=null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        group=(RadioGroup)findViewById(R.id.group);        button01=(RadioButton)findViewById(R.id.button01);        button02=(RadioButton)findViewById(R.id.button02);        checkbox01=(CheckBox)findViewById(R.id.checkbox01);        checkbox02=(CheckBox)findViewById(R.id.checkbox02);        checkbox03=(CheckBox)findViewById(R.id.checkbox03);                group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(button01.getId()==checkedId){Toast.makeText(Activity01.this,"famle",Toast.LENGTH_SHORT).show();}else if(button02.getId()==checkedId){Toast.makeText(Activity01.this, "male", Toast.LENGTH_SHORT).show();}}});                checkbox01.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if(isChecked){Toast.makeText(Activity01.this, "run", Toast.LENGTH_SHORT).show();}else{Toast.makeText(Activity01.this, "norun", Toast.LENGTH_SHORT).show();}}        });                checkbox02.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){     @Override     public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {     if(isChecked){     Toast.makeText(Activity01.this, "swim", Toast.LENGTH_SHORT).show();     }     else{     Toast.makeText(Activity01.this, "noswim", Toast.LENGTH_SHORT).show();     }     }             });                checkbox03.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){     @Override     public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {     if(isChecked){     Toast.makeText(Activity01.this, "read", Toast.LENGTH_SHORT).show();     }     else{     Toast.makeText(Activity01.this, "noread", Toast.LENGTH_SHORT).show();     }     }             });    }}

3、运行效果:


1 0
原创粉丝点击