Android 自定義AlertDialog對(duì)話框樣式
實(shí)際的項(xiàng)目開(kāi)發(fā)當(dāng)中,經(jīng)常需要根據(jù)實(shí)際的需求來(lái)自定義AlertDialog。最近在開(kāi)發(fā)一個(gè)WIFI連接的功能,點(diǎn)擊WIFI需要彈出自定義密碼輸入框。在此權(quán)當(dāng)記錄
效果圖

點(diǎn)擊首頁(yè)的Button即跳出對(duì)話框,顯示W(wǎng)IFI信息(TextView),密碼輸入框(EditText),取消和連接按鈕(Button)
實(shí)現(xiàn)
根據(jù)自己實(shí)際的需求,為AlertDialog創(chuàng)建一個(gè)布局,在此我需要定義一個(gè)如圖所示的WIFI密碼輸入框,故在 res/layout 目錄下建立一個(gè) dialog_layout.xml 文件。
在該布局中,定義一個(gè)TextView顯示wifi名稱(chēng),一條分割線,一個(gè)EditText用于密碼輸入,以及兩個(gè)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 布局,并在這里做我們的邏輯操作
聲明構(gòu)造方法,傳入 Context
在 onCreate() 中加載布局,獲取 View,為按鈕設(shè)置點(diǎn)擊事件
這邊尤其要注意一個(gè)問(wèn)題,在 Dialog 中,定義 EditText 后,在彈出框中點(diǎn)擊 EditText 彈不出鍵盤(pán)來(lái)進(jìn)行輸入,故這里要用 this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM) 保證鍵盤(pán)能彈出以用來(lái)輸入密碼
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能彈出鍵盤(pán)
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;
}
}
}
調(diào)用的話就簡(jiǎn)單了,new 一個(gè) WifiDialog對(duì)象,并調(diào)用 show() 方法即可。這里在 MainActivity 簡(jiǎn)單聲明一個(gè) Button,設(shè)置點(diǎn)擊事件,彈出對(duì)話框。
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();
}
}
}
總結(jié)
以上所述是小編給大家?guī)?lái)的Android 自定義AlertDialog對(duì)話框,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言!
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能
- Android修改源碼解決Alertdialog觸摸對(duì)話框邊緣消失的問(wèn)題
- Android對(duì)話框AlertDialog.Builder使用方法詳解
- ANDROID中自定義對(duì)話框AlertDialog使用示例
- android自定義AlertDialog對(duì)話框
- Android Alertdialog(實(shí)現(xiàn)警告對(duì)話框)
- Android開(kāi)發(fā)之AlertDialog實(shí)現(xiàn)彈出對(duì)話框
相關(guān)文章
android暫?;蛲V蛊渌魳?lè)播放器的播放實(shí)現(xiàn)代碼
來(lái)自android自帶的music源碼,下面是廣播接收的代碼,通過(guò)發(fā)送廣播來(lái)控制音樂(lè)的播放,停止等2013-11-11
Android學(xué)習(xí)小結(jié)之Activity保存和恢復(fù)狀態(tài)
這篇文章主要介紹了Activity狀態(tài)保存和恢復(fù)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
Android N 7.0中報(bào)錯(cuò):android.os.FileUriExposedException的解決方法
這篇文章主要給大家介紹了關(guān)于在Android N 7.0中報(bào)錯(cuò):android.os.FileUriExposedException的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-05-05
Android仿微信錄音功能(錄音后的raw文件轉(zhuǎn)mp3文件)
這篇文章主要介紹了Android中仿微信錄音功能錄音后的raw文件轉(zhuǎn)mp3文件,本文通過(guò)實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2019-11-11
Android編程中ViewPage判斷左右滑動(dòng)方向的方法
這篇文章主要介紹了Android編程中ViewPage判斷左右滑動(dòng)方向的方法,涉及Android中ViewPage針對(duì)滑動(dòng)判定的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-10-10
Android編程實(shí)現(xiàn)仿優(yōu)酷圓盤(pán)旋轉(zhuǎn)菜單效果的方法詳解【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿優(yōu)酷圓盤(pán)旋轉(zhuǎn)菜單效果的方法,涉及Android界面布局及事件響應(yīng)相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-08-08
Flutter Navigator路由傳參的實(shí)現(xiàn)
本文主要介紹了Flutter Navigator路由傳參的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

