Android编程双重单选对话框布局实现与事件监听方法示例

本文实例讲述了Android编程双重单选对话框布局实现与事件监听方法分享给大家供大家参考,具体如下:

首先是自定义XML布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:padding="@dimen/dialog"
  >
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/choice1"
    android:textColor="@color/green"
    android:textSize="@dimen/text"/>
  <RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/radiogroup1">
    <RadioButton
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/kind"
      android:id="@+id/radio1"
      android:checked="true"
      />
    <RadioButton
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/attribute"
      android:id="@+id/radio2"/>
  </RadioGroup>
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/choice2"
    android:textColor="@color/green"
    android:textSize="@dimen/text"/>
  <RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/radiogroup2">
    <RadioButton
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/area"
      android:id="@+id/radio3"
      android:checked="true"/>
    <RadioButton
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/count"
      android:id="@+id/radio4"/>
  </RadioGroup>
</LinearLayout>

效果图如下

引用布局的对话框和监听如下:

LayoutInflater layoutInflater = LayoutInflater.from(MainPlan.this);
View self = layoutInflater.inflate(R.layout.multichoicedialog,null);//引入对话框布局
final RadioGroup radioGroup1 = (RadioGroup) self.findViewById(R.id.radiogroup1);
final RadioGroup radioGroup2 = (RadioGroup) self.findViewById(R.id.radiogroup2);
new AlertDialog.Builder(MainPlan.this)//MainPlan是当前activity
   .setView(self)
   .setonCancelListener(new DialogInterface.OnCancelListener() {
     @Override
     public void onCancel(DialogInterface dialog) {
       dialog.dismiss();
     }
   })
   .setPositiveButton("确定",new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog,int which) {
       if (radioGroup1.getCheckedRadioButtonId() == R.id.radio1) {
         if (radioGroup2.getCheckedRadioButtonId() == R.id.radio3) {
         } else {//处理各种事件
         }
       } else {
         if (radioGroup2.getCheckedRadioButtonId() == R.id.radio3) {
         } else {
         }
       }
     }
   })
   .show();

运行之后的图如下所示

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...