android基本 UI(三)—单选RadioButton与复选按钮CheckBox
来源:互联网 发布:技术支持网络题 编辑:程序博客网 时间:2024/05/22 15:16
源码下载:点击打开链接
(一)单选按钮RadioButton
单选按钮RadioButton,是仅可以选择一个选项的控件,继承自android.widget.CompoundButton,在android.widget包中。
单选按钮要声明在RadioGroup中,RadioGroup是RadioButton的承载体,程序运行时不可见,应用程序中可能包含一个或多个RadioGroup,RadioGroup是线性布局LinearLayout的子类。
单选按钮状态更改的监听,是要给它的RadioGroup添加setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener)监听器。注意监听器类型和复选按钮CheckBox是不相同的。
单选按钮的通常用法:
1、用xml描述的RadioGroup和RadioButton应用的界面设计
<?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"
>
<RadioGroup android:id="@+id/radioGroup"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java" />
<RadioButton android:id="@+id/dotNet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dotNet" />
<RadioButton android:id="@+id/php"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PHP" />
</RadioGroup>
</LinearLayout>
2、引用处理程序
public void onCreate(Bundle savedInstanceState) {
......
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId);
Log.i(TAG, String.valueOf(radioButton.getText()));
}
});
}
RadioButton 和RadioGroup常用的方法及说明:
如下代码:
RadioGroup.check(R.id.dotNet);//将id名为dotNet的单选框设置成选中状态。
(RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());//获取被选中的单选框。
RadioButton.getText( );//获取单选框的值
(二)复选按钮CheckBox
复选按钮CheckBox,是一个同时可以选择多个选项的控件,继承自android.widget.CompoundButton,在android.widget包中。
复选按钮CheckBox的通常用法:
1、用xml描述的CheckBox应用界面设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<CheckBox android:id="@+id/checkboxjava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java" />
<CheckBox android:id="@+id/checkboxdotNet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dotNet" />
<CheckBox android:id="@+id/checkboxphp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PHP" />
<Button android:id="@+id/checkboxButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取值" />
</LinearLayout>
2、 引用xml描述的代码处理:
public class CheckBoxActivity extends Activity {
private static final String TAG = "CheckBoxActivity";
private List<CheckBox> checkboxs = new ArrayList<CheckBox>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox);
checkboxs.add((CheckBox) findViewById(R.id.checkboxdotNet));
checkboxs.add((CheckBox) findViewById(R.id.checkboxjava));
checkboxs.add((CheckBox) findViewById(R.id.checkboxphp));
checkboxs.get(1).setChecked(true);//设置成选中状态
for(CheckBox box : checkboxs){
box.setOnCheckedChangeListener(listener);
}
Button button = (Button)findViewById(R.id.checkboxButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
List<String> values = new ArrayList<String>();
for(CheckBox box : checkboxs){
if(box.isChecked()){
values.add(box.getText().toString());
}
}
Toast.makeText(CheckBoxActivity.this, values.toString(), 1).show();
}
});
}
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CheckBox checkBox = (CheckBox) buttonView;
Log.i(TAG, "isChecked="+ isChecked +",value="+ checkBox.getText());//输出单选框的值
}
};
}
- android基本 UI(三)—单选RadioButton与复选按钮CheckBox
- android单选按钮(RadioButton)、复选框(CheckBox)
- Android组件复习之基本组件02—单选按钮(RadioButton)和复选框(CheckBox)
- Android组件复习之基本组件02—单选按钮(RadioButton)和复选框(CheckBox)
- UI控件之RadioButton(单选按钮)&Checkbox(复选按钮)
- android-UI组件实例大全(四)------单选按钮(RadioButton)和复选框(CheckBox)
- Android-单选按钮RadioButton和复选框checkbox
- Android练习-----单选按钮(RadioButton) && 复选框(CheckBox)
- Android--(11)--解读单选(RadioButton)和复选(CheckBox)按钮
- android 复选单选按钮 CheckBox和RadioButton
- RadioButton(单选按钮)+Checkbox(复选框)
- Android用户界面基础之CheckBox(复选按钮)、RadioButton(单选按钮)学习
- 单选按钮(RadioButton)与 复选框(CheckBox)的功能与 用法
- 单选按钮(RadioButton)与复选框(CheckBox)的功能与用法
- Android基础入门教程——2.3.5.RadioButton(单选按钮)&Checkbox(复选框)
- Android课堂笔记——RadioButton(单选按钮)&Checkbox(复选框)
- Android UI 详解之单选(RadioButton)和复选(CheckBox)
- android学习笔记(三)基础UI组件1——按钮,文本框,CheckBox,Radiobutton介绍
- 黑马程序员——Objective-C——Foundation
- IOS 应用程序生命周期
- UFT关联Lib
- Java读写文件夹下的txt文件,已读的txt文件修改后缀名为.bat。
- STM32L151 的ADC的模拟看门狗中断和溢出中断
- android基本 UI(三)—单选RadioButton与复选按钮CheckBox
- linux驱动中probe函数如何调用
- DOM笔记(一):HTMLDocument接口
- Remove Duplicates from Sorted List II
- JS代码网
- Android如何使用API
- Java线程(十):CyclicBarrier-用路障实现分阶段线程并发
- 初学MongoDB(三)——c++ (java)接口
- 求1-10000之间的同构数