小左闲谈

Mars视频S01E10_Radio单选按钮RadioButton作业

当前位置:首页 » 笔记 » 本页
In 笔记  @2015-04-29

Mars视频S01E10_Radio单选RadioButton作业

实现功能:当选中第一组按钮中的maleButton的时候,会自动选中handsomeButton,当选中femaleButton的时候,会自动选中beautifulButton。
程序运行:
S01E10_Radio程序运行情况

后台情况:
S01E10_Radio后台情况

在本集视频的最后,Mars留下作业,要求两组单选按钮,当第一组里面的一个按钮被选中后,触发第二组里面的一个指定按钮会自动选中,要实现这个功能,视频中讲了监听RadioGroup中选中按钮的Id并且做出相应操作,那么我们只需要以同样的方法获取第一组里面被选中的按钮的Id,比对后,讲第二组中的指定按钮设置为选中就可以了,通过查找可以知道将RadioButton设置为选中的方法是setChecked(true)。

布局文件activity_main.xml:



    
        
        
        
    
    
        
        
        
    


代码文件MainActivity.java:


package com.marschen.s01_e10_radio;

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

public class MainActivity extends Activity {
	
	private RadioGroup radioGroup01;
	private RadioGroup radioGroup02;
	private RadioButton femaleButton;
	private RadioButton maleButton;
	private RadioButton beautifulButton;
	private RadioButton handsomeButton;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		radioGroup01 = (RadioGroup)findViewById(R.id.radioGroupId01);
		radioGroup02 = (RadioGroup)findViewById(R.id.radioGroupId02);
		femaleButton = (RadioButton)findViewById(R.id.femaleButtonId);
		maleButton = (RadioButton)findViewById(R.id.maleButtonId);
		beautifulButton = (RadioButton)findViewById(R.id.beautifulButtonId);
		handsomeButton = (RadioButton)findViewById(R.id.handsomeButtonId);
		
		
		
		
		RadioGroupListener listener = new RadioGroupListener();
		radioGroup01.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");
				beautifulButton.setChecked(true);
				System.out.println("选中了beautiful");
			}
			else if(checkedId == maleButton.getId()){
				System.out.println("选中了male");
				handsomeButton.setChecked(true);
				System.out.println("选中了handsome");
			}
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

若无特殊声明,表示本文由:小左闲谈.原创
链接地址: http://blog.m1910.com/archives/1094.html
版权所有 © 转载时请以链接形式注明作者和原始出处!
标签: , , ,   评论: 2枚 

已经有 2 枚评论. 发表 »