单选框的用法即几种常见对话框

一、单选框

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; // 默认值为0
		switch (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) {

		// 构建AlertDialog
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("警告");//设定标题
		builder.setMessage("世界上最遥远的距离是没有网络");//设定内容
		builder.setPositiveButton("确定", new OnClickListener() {
<span style="white-space:pre">		</span>//固定写法:确定按钮,在左边
			@Override
			public void onClick(DialogInterface dialog, int which) {

				System.out.println("点击了确定按钮 执行的逻辑");

			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {
		//固定写法:取消按钮,在右边
			@Override
			public 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="https://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) {

		// 构建AlertDialog
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("请选择您喜欢的课");
		final String items[] = { "Android", "ios", "php", "c", "C++", "html" };
		builder.setSingleChoiceItems(items, -1, new OnClickListener() {

			@Override
			public 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) {
		// 构建AlertDialog
		AlertDialog.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() {

					@Override
					public void onClick(DialogInterface dialog, int which,
							boolean isChecked) {

					}
				});
		builder.setPositiveButton("确定", new OnClickListener() {

			@Override
			public 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();


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值