RadioGroup组与onCheckedChanged事件

setOnCheckedChangeListener监听check事件

clearCheck选择状态消除

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <!--第一個TextView -->
  <TextView
    android:id="@+id/myTextView"
    android:layout_width="228px"
    android:layout_height="49px"
    android:text="@string/str_radio_question1"
    android:textSize="30sp"
    android:layout_x="37px"
    android:layout_y="3px"
  />
  <!--建立一個RadioGroup -->
  <RadioGroup
    android:id="@+id/myRadioGroup"
    android:layout_width="137px"
    android:layout_height="216px"
    android:orientation="vertical"
    android:layout_x="3px"
    android:layout_y="54px"
    >
    <!--第一個RadioButton -->
    <RadioButton
      android:id="@+id/myRadioButton1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tr_radio_op1"
    />
    <!--第二個RadioButton -->
    <RadioButton
      android:id="@+id/myRadioButton2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/tr_radio_op2"
    />

    <Button
        android:onClick="bt"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    </RadioGroup>  

</AbsoluteLayout>
package com.example.radiogroup;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
	public TextView mTextView1;
	public RadioGroup mRadioGroup1;
	public RadioButton mRadio1,mRadio2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		 mTextView1 = (TextView) findViewById(R.id.myTextView);
		 mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
		 mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
		 mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2); 

		 mRadioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(mRadio1.getId() == checkedId){
					mTextView1.setText("nan");
				}else{
					mTextView1.setText("nv");
				}
			}
		});
	}
	public void bt(View view){
		//将选择状态消除掉
		mRadioGroup1.clearCheck();
	}
}
时间: 2024-08-10 17:52:38

RadioGroup组与onCheckedChanged事件的相关文章

iOS监听tableView组头切换事件

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 组头将要出现的时候系统会调用: - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section 组头出现的时候系统

[转]android学习----基础UI编程(四)

CheckBox 的使用 RadioButton 的使用 12. CheckBox 的使用 1)通过只含有一个CheckBox的实例来学习CheckBox的使用 示例代码 ① 创建新工程② 在string.xml 中添加字符串 <?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">Ex_Ctrl_4</string

安卓应用的界面编程(3)

第二组UI组件:TextView及其子类 1. TextView(不允许用户编辑文本内容)直接继承了View,同时是EditText(允许用户编辑文本内容)/Button两个UI组件类的父类.TextView的作用就是在界面上显示文本(类似JLabel) 下面是TextView的几个使用例子 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientatio

μC/OSIII——任务同步(事件标志组)

使用情况 当任务需要与多个事件的发生同步,可以使用事件标志组.有两种情况: 或同步——等待多个事件发生,任何一个事件发生,任务就发生(同步) 与同步——等待多个事件发生,当所有事件都发生,任务就发生(同步) 使用方法 事件标志组服务函数的实现代码在os_flag.c文件中,在编译时,将os_cfg.h文件中的配置常数OS_CFG_FLAG+EN设为1就可启用这些服务函数. 常用的事件标志组的服务函数有: OSFlagCreate() 创建事件标志组 OSFlagPend()    等待事件标志

事件标志组

---恢复内容开始--- 事件标志组,顾名思义,就是若干个事件标志的组合,代表若干个事件是否发生,通常用于集合两个或两个以上事件的状态. 如果想要使用事件标志组,就必须事先使能事件标志组.消息队列的使能位于"os_cfg.h" /* ----------------------------- EVENT FLAGS --------------------------- */ #define OS_CFG_FLAG_EN 1u //使能/禁用事件标志组 #define OS_CFG_F

Android RadioGroup和RadioButton详解

实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中:当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个.并用setOnCheckedChangeListener来对单选按钮进行监听 1 RadioGroup相关属性: 2 RadioGroup.getC

Android控件(2)RadioButton&amp;RadioGroup

抄自: http://www.cnblogs.com/wt616/archive/2011/06/20/2085531.html 学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握RadioGroup选中状态变换的事件(监听器) RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单个Che

Android控件系列之RadioButton&amp;RadioGroup

学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握RadioGroup选中状态变换的事件(监听器) RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后,通过点击可以变为未选中 2.一组RadioButton,只能同时选中一个 一组CheckBox,能同时选中多个

Android 学习笔记---获取RadioGroup的选定值

1,获取RadioGroup控件: RadioGroup radioGroup = (RadioGroup)findViewById(R.id.myRadioGroup); 2,获取RadioButton控件; RadioButton radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId()); 3,获取选中的radio的值: String text = radioButton.getText().t