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

 更新时间:2017年10月27日 11:08:55   作者:GISuuser  
这篇文章主要介绍了Android编程双重单选对话框布局实现与事件监听方法,涉及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程序自动更新功能模块的实现方法【附完整demo源码下载】

    Android程序自动更新功能模块的实现方法【附完整demo源码下载】

    这篇文章主要介绍了Android程序自动更新功能模块的实现方法,具备完整的自动检测更新及下载、安装等功能,并附带完整的demo源码供大家下载参考,需要的朋友可以参考下
    2016-08-08
  • Android自定义Banner轮播效果

    Android自定义Banner轮播效果

    这篇文章主要为大家详细介绍了Android自定义Banner轮播效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • 创建Android守护进程实例(底层服务)

    创建Android守护进程实例(底层服务)

    这篇文章主要介绍了创建Android守护进程实例(底层服务),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android仿新浪微博、QQ空间等帖子显示(2)

    Android仿新浪微博、QQ空间等帖子显示(2)

    这篇文章主要为大家详细介绍了Android仿新浪微博、QQ空间等帖子显示,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Android自定义View实现游戏摇杆键盘的方法示例

    Android自定义View实现游戏摇杆键盘的方法示例

    Android进阶过程中有一个绕不开的话题——自定义View。最近在做项目中又遇到了,所以下面这篇文章主要给大家介绍了利用Android自定义View实现游戏摇杆键盘的相关资料,操作方式类似王者荣耀的摇杆操作,文中通过示例代码介绍的非常详细,需要的朋友们下面来一起看看吧。
    2017-07-07
  • Android Canvas的drawText()与文字居中方案详解

    Android Canvas的drawText()与文字居中方案详解

    这篇文章主要给大家介绍了关于Android Canvas的drawText()与文字居中方案的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-12-12
  • Android发送邮件的方法实例详解

    Android发送邮件的方法实例详解

    这篇文章主要介绍了Android发送邮件的方法,结合实例形式详细分析了Android邮件发送的相关技巧与具体实现步骤,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-01-01
  • Android4.4+ 实现半透明状态栏(Translucent Bars)

    Android4.4+ 实现半透明状态栏(Translucent Bars)

    这篇文章主要为大家详细介绍了Android4.4+ 实现半透明状态栏,对状态栏(Status Bar)和下方导航栏(Navigation Bar)进行半透明处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • Android自定义Material进度条效果

    Android自定义Material进度条效果

    这篇文章主要为大家详细介绍了Android自定义Material进度条效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android中的RecyclerView新组件初步上手指南

    Android中的RecyclerView新组件初步上手指南

    RecyclerView是Android L版本开始采用的一个组件,被人们认为用来代替传统的ListView,下面我们就一起来看一下Android中的RecyclerView新组件初步上手指南
    2016-06-06

最新评论