码迷,mamicode.com
首页 > 移动开发 > 详细

Android中RadionButton与CheckBox的应用

时间:2015-06-04 12:07:53      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:android radiobutton checkbox

//RadioGroup中xml文件的配置
<RadioGroup 
        
        android:id="@+id/radiogroupid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <RadioButton 
            android:id="@+id/femalebutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="female"
            />
        <RadioButton 
            android:id="@+id/malebutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="male"
            />
        
    </RadioGroup>
    
    //RadioGroup中activity中代码片段
    public class MainActivity extends Activity {
	private RadioGroup radiogroup;
	private RadioButton femalebutton,malebutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radiogroup=(RadioGroup)findViewById(R.id.radiogroupid);
        femalebutton=(RadioButton)findViewById(R.id.femalebutton);
        malebutton=(RadioButton)findViewById(R.id.malebutton);
        RadioGroupLis r=new RadioGroupLis();
        radiogroup.setOnCheckedChangeListener(r);
    }
    
    
    class RadioGroupLis implements OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			if(checkedId==femalebutton.getId())
			{
				Toast.makeText(getApplicationContext(), "选中female", Toast.LENGTH_LONG).show();
			}else if(checkedId==malebutton.getId())
			{
				Toast.makeText(getApplicationContext(), "选中male", Toast.LENGTH_SHORT).show();
			}
		}
    	
    	
    	
    }
    
    CheckBox中xml文件
    <CheckBox 
	    android:id="@+id/eatid"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="吃饭"
	    
	    />
	<CheckBox 
	    android:id="@+id/playid"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="游戏"
	    
	    />
	<CheckBox 
	    android:id="@+id/sleepid"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="睡觉"
	    
	    />
    
    //checkbox中的activity文件
    
    public class MainActivity extends Activity {
	private CheckBox eatbox,sleepbox,playbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        eatbox=(CheckBox)findViewById(R.id.eatid);
        sleepbox=(CheckBox)findViewById(R.id.sleepid);
        playbox=(CheckBox)findViewById(R.id.playid);
        onBoxLis listener=new onBoxLis();
        eatbox.setOnClickListener(listener);
        sleepbox.setOnClickListener(listener);
        playbox.setOnClickListener(listener);
        
    }
    
    //onclickListener的使用方法
    class onBoxLis implements OnClickListener{

		@Override
		public void onClick(View v) {
			CheckBox box=(CheckBox)v;
			if(box.isChecked())
			{
			Toast.makeText(getApplicationContext(), "被选中",
				     Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(getApplicationContext(), "未被选中", Toast.LENGTH_LONG).show();
			}
		}
    	
    }


本文出自 “Java大白的战地” 博客,请务必保留此出处http://8023java.blog.51cto.com/10117207/1658339

Android中RadionButton与CheckBox的应用

标签:android radiobutton checkbox

原文地址:http://8023java.blog.51cto.com/10117207/1658339

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!