Android_单选按钮(第一季重制版)

1.单选按钮(RadioButton)的基本概念





2.RadioGroup与RadioButton


3.OnClickListener与OnCheckedChangeListener监听器

eg):

<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=".MainActivity" >

   <RadioGroup 
       android:id="@+id/radioGroupId"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
   
       <RadioButton 
           android:id="@+id/femaleButtonId"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="female"/>
       
       <RadioButton
           android:id="@+id/maleButtonId"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="male"
           />
   </RadioGroup>

</LinearLayout>

package com.marschen.s01_e10_radio;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

	private RadioGroup radioGroup;
	private RadioButton femaleButton; 
	private RadioButton 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.femaleButtonId);
		maleButton = (RadioButton)findViewById(R.id.maleButtonId);
		
		RadioGroupListener listener = new RadioGroupListener();
		radioGroup.setOnCheckedChangeListener(listener);
		
		RadioButtonListener radioButtonListener = new RadioButtonListener();
		femaleButton.setOnCheckedChangeListener(radioButtonListener);
	}


	class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
			System.out.println("isChecked--->" + isChecked);
		}
	}
	
	class RadioGroupListener implements OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			if(checkedId == femaleButton.getId()){
				System.out.println("选中了female");
			}
			else if(checkedId == maleButton.getId()){
				System.out.println("选中了male");
			}
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值