Android 多种dialog的实现方法(推荐)

 更新时间:2018年01月29日 12:09:00   作者:迟暮有话说  
下面小编就为大家分享一篇Android 多种dialog的实现方法(推荐),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

要求:设计如下界面,包括图片按钮、复选按钮、单选按钮、普通按钮,单击按钮弹出对话框,运行效果如下:

string.xml文件源代码:

<resources>
  <string name="app_name">多种弹出对话框</string>
  <string name="dialog_img">图片按钮</string>
  <string name="dialog_radio">单选按钮</string>
  <string name="dialog_box">复选按钮</string>
  <string name="close">关闭</string>
</resources>

布局文件源代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 <ImageView
   android:id="@+id/img"
   android:layout_height="100dp"
   android:layout_width="100dp"
   android:src="@drawable/aa"/>
  <CheckBox
    android:id="@+id/Checkbox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chinese"/>
  <CheckBox
    android:id="@+id/Checkbox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="English"
    android:checked="true"/>
  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="复选按钮确定"/>
  <RadioGroup
    android:id="@+id/radioGroup"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RadioButton
      android:id="@+id/male"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="male" />
    <RadioButton
      android:id="@+id/female"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="true"
      android:text="female"/>
  </RadioGroup>
  <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="单选按钮确定"/>
</LinearLayout>

java文件源代码:

package com.example.shiyan1_4;
import android.app.Dialog;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import static android.R.attr.id;
public class Shiyan1_4Activity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shiyan1_4);
    ImageView img = (ImageView) findViewById(R.id.img);
    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    img.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        AlertDialog.Builder dialog1 = new AlertDialog.Builder(Shiyan1_4Activity.this);
        dialog1.setTitle(R.string.dialog_img)
            .setMessage("您点击了图片按钮!")
            .setNegativeButton(R.string.close, null)
            .create()
            .show();
      }
    });
    button1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        AlertDialog.Builder dialog1 = new AlertDialog.Builder(Shiyan1_4Activity.this);
        String str1 = "";
        //获取复选按钮的值
        CheckBox Checkbox1 = (CheckBox)findViewById(R.id.Checkbox1);
        if(Checkbox1.isChecked()){
          str1 += Checkbox1.getText() + "is selected !";
        }
        CheckBox Checkbox2 = (CheckBox)findViewById(R.id.Checkbox2);
        if(Checkbox2.isChecked()){
          str1 += Checkbox2.getText() + " is selected !\n";
        }
        dialog1.setTitle(R.string.dialog_box)
            .setMessage(str1)
            .setNegativeButton(R.string.close, null)
            .create()
            .show();
      }
    });
    button2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        AlertDialog.Builder dialog2 = new AlertDialog.Builder(Shiyan1_4Activity.this);
        String str2 = "";
        //获取单选按钮的值
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        for (int i = 0; i <radioGroup.getChildCount(); i++){
          RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
          if(radioButton.isChecked()){
            str2 += radioButton.getText() + " is selected !";
            break;
          }
        }
        dialog2.setTitle(R.string.dialog_radio)
            .setMessage(str2)
            .setNegativeButton(R.string.close, null)
            .create()
            .show();
      }
    });
  }
}

以上这篇Android 多种dialog的实现方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Android实现图片随手指旋转功能

    Android实现图片随手指旋转功能

    这篇文章主要为大家详细介绍了Android实现图片随手指旋转功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android Studio3安装图文教程

    Android Studio3安装图文教程

    这篇文章主要为大家详细介绍了Android Studio3安装图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • Kotlin StateFlow单数据更新热流设计与使用介绍

    Kotlin StateFlow单数据更新热流设计与使用介绍

    StateFlow当值发生变化,就会将值发送出去,下流就可以接收到新值。在某些场景下,StateFlow比LiveData更适用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-09-09
  • Android Sqlite命令详解(基本命令)

    Android Sqlite命令详解(基本命令)

    这篇文章主要介绍了Android Sqlite命令详解(基本命令)的相关资料,需要的朋友可以参考下
    2015-12-12
  • Android中扫描多媒体文件操作详解

    Android中扫描多媒体文件操作详解

    这篇文章主要介绍了Android中扫描多媒体文件操作详解,本文讲解了Android中的多媒体文件扫描机制、如何扫描一个刚创建的文件、如何扫描多个文件,需要的朋友可以参考下
    2015-01-01
  • Android Coil对比Glide深入分析探究

    Android Coil对比Glide深入分析探究

    这篇文章主要介绍了Android Coil对比Glide,Coil是Android上的一个全新的图片加载框架,它的全名叫做coroutine image loader,即协程图片加载库
    2023-02-02
  • Android MPAndroidChart绘制原理

    Android MPAndroidChart绘制原理

    这篇文章主要介绍了Android MPAndroidChart绘制原理,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • Android Intent发送广播消息实例详解

    Android Intent发送广播消息实例详解

    这篇文章主要介绍了Android Intent发送广播消息实例详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • OpenHarmony实现屏幕亮度动态调节方法详解

    OpenHarmony实现屏幕亮度动态调节方法详解

    大家在拿到dayu之后,都吐槽说,会经常熄屏,不利于调试,那么有没有一种办法,可以让app不熄屏呢,答案是有的,今天我们就来揭秘一下,如何控制屏幕亮度
    2022-11-11
  • Android开发之时间日期组件用法实例

    Android开发之时间日期组件用法实例

    这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
    2014-08-08

最新评论