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