单选框的用法即几种常见对话框
来源:互联网 发布:别墅网络覆盖方案 编辑:程序博客网 时间:2024/06/07 03:47
一、单选框
RadioGroup的组事件,可以将各自不同的RadioButton,设定与同一个RadioGroup组里的按钮,做出单一选择。
RadioGroup中使用类组单选按钮。如果我们选中一个单选按钮属于一个单选按钮组,它会自动取消选中同一组内的任何先前检查的单选按钮。
RadioGroup 属性:继承自android.view.View类
android:checkedButton :这是子单选按钮应该在默认情况下此单选组内进行检查的ID。一般不用。
android:orientation="horizontal" 设置单选框布局方向。horizontal为横向 vertical为垂直方向
RadioButton的属性:
android:checked="true" 设置单选框为选中,默认为没有选中。只能在一个RadioButton中设定(单选)。
布局例子:
<span style="font-size:18px;"><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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.rp.MainActivity" > <EditText android:id="@+id/et_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/name" /> <RadioGroup android:id="@+id/rg_Group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /> <RadioButton android:id="@+id/rb_female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/rb_margin" android:checked="true" android:text="@string/female" /> <RadioButton android:id="@+id/rb_other" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/rb_margin" android:text="@string/other" /> </RadioGroup> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="@string/compute"/></LinearLayout></span>布局显示如下图:
也可以设定单选框的监听:
RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId)
}
int checkedRadioButtonId = rg_Group.getCheckedRadioButtonId();判断选择的单选框的id。
举例:
<span style="font-size:18px;">// [2]判断选中性别int checkedRadioButtonId = rg_Group.getCheckedRadioButtonId();// [3]判断一下具体选中的性别int sex = 0; // 默认值为0switch (checkedRadioButtonId) {case R.id.rb_male: // 选中的是男sex = 1;break;case R.id.rb_female: // 选中的是女sex = 2;break;case R.id.rb_other: // 代表选中的是人妖sex = 3;break;}// [4]判断性别if (sex == 0) {Toast.makeText(mContext, "亲 请选择性别 ", 0).show();return;}</span>
</pre><pre name="code" class="java"><span style="font-size:18px;">下面为监听:</span>
<span style="font-size:18px;">public class RadioTest extends Activity { TextView textview; RadioGroup radiogroup; RadioButton radio1,radio2,radio3,radio4; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textview=(TextView)findViewById(R.id.textview1); radiogroup=(RadioGroup)findViewById(R.id.radiogroup1); radio1=(RadioButton)findViewById(R.id.radiobutton1); radio2=(RadioButton)findViewById(R.id.radiobutton2); radio3=(RadioButton)findViewById(R.id.radiobutton3); radio4=(RadioButton)findViewById(R.id.radiobutton4); radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(checkedId==radio2.getId()) { DisplayToast("正确答案:"+radio2.getText()+",恭喜你,回答正确!"); }else { DisplayToast("请注意,回答错误!"); } } }); } </span>
二、常用的对话框:
1、普通对话框
先看图片:
我现在布局里面定义个一个监控的button按钮,主界面点击“普通对话框”会弹出普通对话框
<span style="font-size:18px;">// 点击按钮 弹出一个普通对话框public void click1(View v) {// 构建AlertDialogAlertDialog.Builder builder = new Builder(this);builder.setTitle("警告");//设定标题builder.setMessage("世界上最遥远的距离是没有网络");//设定内容builder.setPositiveButton("确定", new OnClickListener() {<span style="white-space:pre"></span>//固定写法:确定按钮,在左边@Overridepublic void onClick(DialogInterface dialog, int which) {System.out.println("点击了确定按钮 执行的逻辑");}});builder.setNegativeButton("取消", new OnClickListener() {//固定写法:取消按钮,在右边@Overridepublic void onClick(DialogInterface dialog, int which) {System.out.println("点击了取消按钮");}});// 最后一步一定要记得 show出来builder.show();}</span>
<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;"><span style="font-size:18px;"> builder.setNeutralButton 设置第三个按钮的普通对话框,在中间,有时会用到,跟上面用法一样</span></span>
<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;"><span style="font-size:18px;"></span></span>
<span style="font-family:Consolas, Courier New, Courier, mono, serif;font-size:18px;"><span style="line-height: 18px;">2,单选对话框</span></span>
<span style="font-family:Consolas, Courier New, Courier, mono, serif;font-size:18px;"><span style="line-height: 18px;">先看图片:</span></span>
<span style="font-family:Consolas, Courier New, Courier, mono, serif;font-size:18px;"><span style="line-height: 18px;"><img src="http://img.blog.csdn.net/20160818212124329?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span></span>
单选框设定一个标题和单选列表,需要一个String 数组传输。代码如下
<span style="font-size:18px;">// 点击按钮 弹出一个单选对话框public void click2(View v) {// 构建AlertDialogAlertDialog.Builder builder = new Builder(this);builder.setTitle("请选择您喜欢的课");final String items[] = { "Android", "ios", "php", "c", "C++", "html" };builder.setSingleChoiceItems(items, -1, new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 取出点中的条目String item = items[which];Toast.makeText(getApplicationContext(), item, 1).show();// 关闭当前对话框dialog.dismiss();}});// 最后一步一定要记得 show出来builder.show();}</span>
<span style="font-size:18px;">builder.setSingleChoiceItems(items, -1, new OnClickListener()</span>
<span style="font-size:18px;"></span>中的-1是代表数组中没有被初始化选中的,如果为0代表数组中第一个元素被初始化选中,1代表数组第二个元素被选、、、、依次类推
3、多选对话框:
先看图片如下
就是比多选框多个几个选择。先看代码如下
<span style="font-size:18px;">// 点击按钮 弹出一个多选对话框public void click3(View v) {// 构建AlertDialogAlertDialog.Builder builder = new Builder(this);builder.setTitle("请选择您喜欢吃的水果");final String items[] = { "榴莲", "苹果", "葡萄", "香蕉", "黄瓜", "火龙果", "荔枝","梨子","西瓜","哈密瓜","猕猴桃"};final boolean[] checkedItems = { true, false, false, false, false,false, true , false, false, false, false};builder.setMultiChoiceItems(items, checkedItems,new OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which,boolean isChecked) {}});builder.setPositiveButton("确定", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {StringBuffer sb = new StringBuffer();sb.append("你喜欢的水果有:");// [1]把你选中的水果给取出来for (int i = 0; i < checkedItems.length; i++) {if (checkedItems[i]) {// 就证明是选中的String fruit = items[i];sb.append(fruit + " ");}}Toast.makeText(getApplicationContext(), sb.toString(), 1).show();// 关闭对话框dialog.dismiss();}});</span>相比单选,初始化时,多了个boolean数组,根据true false来判断每一项是否被初始选中。还有不同的是,多选是根据一个确定按钮来触发点击事件。
4、进度条对话框
先看效果:
A,圆圈模式:
B,横向进度条模式
<span style="font-size:18px;">//进度条对话框public void click4(View v) {//与进度相关的控件都可以直接在子线程更新ui final ProgressDialog dialog = new ProgressDialog(this);dialog.setTitle("正在玩命加载ing");dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//横向进度条</span>
<span style="font-size:18px;"><span style="white-space:pre"></span>//dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 圆圈模式dialog.show();//设置进度条的最大值 dialog.setMax(100);new Thread(){public void run() {for (int i = 0; i <= 100; i++) {//设置当前的进度 //不需要catch异常SystemClock.sleep(50);//睡眠50毫秒dialog.setProgress(i);}//关闭对话框dialog.dismiss();};}.start();}}</span>
如上代码中,记得耗时操作要在子线程中运行。
总结:学会单选框的布局和监听。掌握几种常用的对话框,记得最后要关闭对话框 dialog.dismiss();
- 单选框的用法即几种常见对话框
- 常见的对话框Dialog
- 常见对话框与自定义的的对话框
- 打开对话框的用法
- 常见的android弹出对话框
- Android中常见的对话框
- 对话框的基本的用法
- 常见对话框
- 常见对话框
- 常见对话框
- 常见对话框
- cookies的常见用法
- Intent的常见用法
- jad 常见的用法
- Intent的常见用法
- const的常见用法
- const的常见用法
- const的常见用法
- 智能指针之shared_ptr
- LintCode算法题解
- UVA 11021 Tribles
- 使用getopt命令解析shell脚本的命令行选项
- canvas的drawText 如何将文字绘制在控件的中心
- 单选框的用法即几种常见对话框
- 带符号整数的除法与余数
- lightoj 1369 - Answering Queries (数学、规律、水)
- linux proc文件系统使用之单次读
- 浅谈EditText
- csu 1416 Practical Number(数论,结论题)
- 不等概率采样
- 暑期dp46道(43)--HDOJ 1159 最长公共子序列(可不连续)
- 对数组的验证