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

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() {
			
			@Override
			public 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(){
			@Override
			public 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、运行效果:


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值