Activity常用控件——09

RadioGroup

用于创建一组选中状态相互排斥的单选按钮组。RadioGroup和 RadioButton通常是放在一起使用的


RadioButton

单选按钮是一种双状态的按钮,可以选择或不选中。


CheckBox

CheckBox是有双状态按钮的复选框,可以选中也可 以不选中。

Toast

快速的返回少量信息,浮于程序上,不取得焦点


import java.util.ArrayList;  
  
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;  
import android.widget.RadioGroup.OnCheckedChangeListener;  
import android.widget.TextView;  
  
public class RadioTest extends Activity {  
    private ArrayList<String> sportList = new ArrayList<String>();  
    private TextView textView;  
    private RadioGroup genderGroup=null;  
    private RadioButton maleButton=null;  
    private RadioButton femaleButton=null;  
    private CheckBox runBox=null;  
    private CheckBox swimBox=null;  
    private CheckBox readBox=null;    
      
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        textView = (TextView)findViewById(R.id.textView);  
        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);  
        maleButton = (RadioButton)findViewById(R.id.maleButton);  
        femaleButton = (RadioButton)findViewById(R.id.femaleButton);  
        run = (CheckBox)findViewById(R.id.run);  
        swim = (CheckBox)findViewById(R.id.swim);  
        read = (CheckBox)findViewById(R.id.read);  
          
        run.setOnCheckedChangeListener(new OnChecked());  
        swim.setOnCheckedChangeListener(new OnChecked());  
        read.setOnCheckedChangeListener(new OnChecked());  
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
              
            public void onCheckedChanged(RadioGroup group, int checkedId) {  
                // TODO Auto-generated method stub  
                if(femaleButton.getId() == checkedId){  
                    gender = "female";  
                }  
                else if(maleButton.getId() == checkedId){  
                    gender = "male";  
                }  
            }  
        });  
          
          
          
    }  
    class OnChecked implements CompoundButton.OnCheckedChangeListener{  
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
            // TODO Auto-generated method stub  
            String str = "";  
              
            if(buttonView == run){  
                str = "runBox";  
            }  
            else if(buttonView == swim){  
                str = "swimBox
";     
            }  
              
            else if(buttonView == read){  
                str = "readBox";  
            }  
                  
            if(isChecked){  
                if(!sportList.contains(str)){  
                    sportList.add(str);  
                }  
                  
            }  
              
            else if(!isChecked){  
                if(sportList.contains(str)){  
                    sportList.remove(str);  
                }  
                  
            }  
            String text ="gender: " + gender + "   sport: ";  
            for(String s : sportList){  
                text += s + "  ";  
            }  
              
            textView.setText(text);  
            Toast.makeText(RadioTest.this, text, Toast.LENGTH_LONG).show();  
        }  
          
    }  
  
}  

配置文件

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.activity_06.RadioTest" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <RadioGroup
        android:id="@+id/genderGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
    <RadioButton
        android:id="@+id/femaleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/female"
        />
     <RadioButton
        android:id="@+id/maleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/male"
        />
     </RadioGroup>
     <CheckBox 
         android:id="@+id/swimBox"
         android:layout_width="wrap_content"
      	  android:layout_height="wrap_content"
   	     android:text="@string/swimBox"         
         />
     <CheckBox 
         android:id="@+id/readBox"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/readBox"
         />
      <CheckBox 
         android:id="@+id/runBox"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/runBox"         
         />

</LinearLayout>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值