Android 自定义AlertDialog对话框样式

 更新时间:2017年09月16日 15:55:48   投稿:mrr  
实际的项目开发当中,经常需要根据实际的需求来自定义AlertDialog。最近在开发一个WIFI连接的功能,点击WIFI需要弹出自定义密码输入框,具体代码大家参考下本文

实际的项目开发当中,经常需要根据实际的需求来自定义AlertDialog。最近在开发一个WIFI连接的功能,点击WIFI需要弹出自定义密码输入框。在此权当记录

效果图

点击首页的Button即跳出对话框,显示WIFI信息(TextView),密码输入框(EditText),取消和连接按钮(Button)

实现

根据自己实际的需求,为AlertDialog创建一个布局,在此我需要定义一个如图所示的WIFI密码输入框,故在 res/layout 目录下建立一个 dialog_layout.xml 文件。

在该布局中,定义一个TextView显示wifi名称,一条分割线,一个EditText用于密码输入,以及两个Button用于取消与连接

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="300dp"
  android:layout_height="180dp"
  android:orientation="vertical">
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:gravity="center"
    android:text="WIFI"
    android:textSize="18sp" />
  <View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:background="#F5F5F5" />
  <EditText
    android:id="@+id/et_passwd"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:hint="Password"
    android:inputType="numberPassword" />
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="horizontal">
    <Button
      android:id="@+id/btn_cancel"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="@null"
      android:text="取消"
      android:textColor="#1965db"
      android:textSize="16sp" />
    <Button
      android:id="@+id/btn_connect"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:background="@null"
      android:text="连接"
      android:textColor="#1965db"
      android:textSize="16sp" />
  </LinearLayout>
</LinearLayout>

新建 WifiDialog.java 继承 AlertDialog ,并引入刚刚所定义的 dialog_layout.xml 布局,并在这里做我们的逻辑操作

声明构造方法,传入 Context

在 onCreate() 中加载布局,获取 View,为按钮设置点击事件

这边尤其要注意一个问题,在 Dialog 中,定义 EditText 后,在弹出框中点击 EditText 弹不出键盘来进行输入,故这里要用 this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM) 保证键盘能弹出以用来输入密码

package com.example.test.dialogtest;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
 * Created by AaronPasi on 2017/9/16.
 */
public class WifiDialog extends AlertDialog implements View.OnClickListener {
  EditText mEtPasswd;
  Button mBtnCancel, mBtnConnect;
  Context mContext;
  public WifiDialog(Context context) {
    super(context);
    mContext = context;
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_layout);
    mEtPasswd = (EditText) findViewById(R.id.et_passwd);
    //保证EditText能弹出键盘
    this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    this.setCancelable(false);
    mBtnCancel = (Button) findViewById(R.id.btn_cancel);
    mBtnCancel.setOnClickListener(this);
    mBtnConnect = (Button) findViewById(R.id.btn_connect);
    mBtnConnect.setOnClickListener(this);
  }
  @Override
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.btn_cancel:
        this.dismiss();
        break;
      case R.id.btn_connect:
        if (TextUtils.isEmpty(mEtPasswd.getText())) {
          Toast.makeText(mContext, "密码不能为空", Toast.LENGTH_SHORT).show();
        } else {
          this.dismiss();
          Toast.makeText(mContext, mEtPasswd.getText().toString(), Toast.LENGTH_SHORT).show();
        }
        break;
      default:
        break;
    }
  }
}

调用的话就简单了,new 一个 WifiDialog对象,并调用 show() 方法即可。这里在 MainActivity 简单声明一个 Button,设置点击事件,弹出对话框。

package com.example.test.dialogtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private Button mDialogBtn;
  private WifiDialog mDialog;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDialogBtn = (Button) findViewById(R.id.btn_dialog);
    mDialogBtn.setOnClickListener(this);
  }
  @Override
  public void onClick(View view) {
    if (view.getId() == R.id.btn_dialog) {
      mDialog = new WifiDialog(this);
      mDialog.show();
    }
  }
}

总结

以上所述是小编给大家带来的Android 自定义AlertDialog对话框,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言!

相关文章

  • android实现上滑屏幕隐藏底部菜单栏的示例

    android实现上滑屏幕隐藏底部菜单栏的示例

    这篇文章主要介绍了android实现上滑屏幕隐藏底部菜单栏的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Android实现歌词渐变色和进度的效果

    Android实现歌词渐变色和进度的效果

    这篇文章主要介绍了Android实现歌词渐变色和进度的效果的相关资料,需要的朋友可以参考下
    2016-03-03
  • Android Flutter实现兴趣标签选择功能

    Android Flutter实现兴趣标签选择功能

    我们在首次使用内容类 App 的时候,不少都会让我们选择个人偏好,通过这些标签选择可以预先知道用户的偏好信息。我们本篇就来看看 Flutter 如何实现兴趣标签的选择,需要的可以参考一下
    2022-11-11
  • Android自定义键盘的实现(数字键盘和字母键盘)

    Android自定义键盘的实现(数字键盘和字母键盘)

    本篇文章主要介绍了Android自定义键盘的实现(数字键盘和字母键盘),具有一定的参考价值,有兴趣的可以了解一下
    2017-08-08
  • 如何利用Kotlin实现极简回调

    如何利用Kotlin实现极简回调

    这篇文章主要给大家介绍了关于如何利用Kotlin实现极简回调的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • Android 消息分发使用EventBus的实例详解

    Android 消息分发使用EventBus的实例详解

    这篇文章主要介绍了Android 消息分发使用EventBus的实例详解的相关资料,在项目中用了许多Handler和broadcast导致代码冗余,显得杂乱无章,这里使用EventBus来实现相同的功能,需要的朋友可以参考下
    2017-07-07
  • Android编程实现禁止状态栏下拉的方法详解

    Android编程实现禁止状态栏下拉的方法详解

    这篇文章主要介绍了Android编程实现禁止状态栏下拉的方法,结合实例形式详细分析了Android状态栏操作相关的函数、属性调用及权限控制设置技巧,需要的朋友可以参考下
    2017-08-08
  • Android实现客户端语音动弹界面实例代码

    Android实现客户端语音动弹界面实例代码

    这篇文章主要介绍了Android实现客户端语音动弹界面实例代码,文章只给大家介绍了控件布局的方法,需要的朋友可以参考下
    2017-11-11
  • Android 获取应用缓存大小与清除缓存的方法

    Android 获取应用缓存大小与清除缓存的方法

    今天小编就为大家分享一篇Android 获取应用缓存大小与清除缓存的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Android项目中实体类entity的作用详解

    Android项目中实体类entity的作用详解

    这篇文章主要介绍了Android项目中实体类entity的作用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04

最新评论