android widget 之RadioGroup RadioButton
RadioGroup + RadioButton 提供了一种多选一的模式。下面是xml 文件:
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text = "请选择你的性别"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rg1">
<RadioButton android:text="男" android:id="@+id/rb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<RadioButton android:text="女" android:id="@+id/rb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<RadioButton android:text="不男不女" android:id="@+id/rb3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RadioGroup>
</LinearLayout>
改配置中定义了一个RadioGroup,而这个Group包括了3个RadioButton。性别对于人来说,只可能是一种,所以适合用RadioButton。需要指出的是,如果RadioButton不放在一个RadioGroup中,是不存在互斥的效果的。
RadioGroup 支持 setOnCheckedChangeListener,也就是说,改Group下面的选择发生变化的时候,RadioGroup可以监听到这种变化。
RadioButton 也支持setOnCheckedChangeListener。 不过他们对应的Listener不同,RadioGroup 是:RadioGroup.OnCheckedChangeListener,而RadioButton是:
CompoundButton.OnCheckedChangeListener.
RadioGroup.getCheckedRadioButtonId(),可以得到选择的RadioButton的ID。
代码如下:
package org.terry;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class RadioBoxDemo extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radiobox);
final RadioGroup rg1 = (RadioGroup)findViewById(R.id.rg1);
RadioButton rb1 = (RadioButton) findViewById(R.id.rb1);
RadioButton rb2 = (RadioButton) findViewById(R.id.rb2);
RadioButton rb3 = (RadioButton) findViewById(R.id.rb3);
Button btn1 = (Button)findViewById(R.id.rbtn1);
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) findViewById(checkedId);
Toast.makeText(getApplicationContext(), "you selected "+rb.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
RadioButton rb = (RadioButton) buttonView;
Toast.makeText(getApplicationContext(), "you selected "+rb.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RadioButton rb = (RadioButton) findViewById(rg1.getCheckedRadioButtonId());
Toast.makeText(getApplicationContext(), "you selected "+rb.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
} }
更多相关文章
- 详解Android中的屏幕方向类型
- 利用virtualbox安装android系统
- Android(安卓)Unable to resolve target 'android-X'
- Android(安卓)LruCache和DiskLruCache相结合打造图片加载框架(仿
- Android里监视数据库的变化[转]
- Android(安卓)studio怎么使用eclipse的快捷键?
- 在windows平台上搭建Android开发环境-图例
- 【已解决】ADT中通过Android(安卓)SDK Manager去安装x86的image
- 2.1搭建Android环境—Android跟我学1.1