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

所属分类: 软件编程 / Android 阅读数: 67
收藏 0 赞 0 分享

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

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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多