ANDROID中自定義對(duì)話框AlertDialog使用示例
在Android開發(fā)中,我們經(jīng)常會(huì)需要在Android界面上彈出一些對(duì)話框,比如詢問(wèn)用戶或者讓用戶選擇。這些功能我們叫它Android Dialog對(duì)話框,AlertDialog實(shí)現(xiàn)方法為建造者模式。AlertDialog中定義的一些對(duì)話框往往無(wú)法滿足我們關(guān)于對(duì)話框的需求,這時(shí)我們就需要通過(guò)自定義對(duì)話框VIEW來(lái)實(shí)現(xiàn)需求,這里我自定義一個(gè)登陸的提示對(duì)話框,效果圖顯示如下:
Layout(alertdialog自定義登陸按鈕)界面代碼:
<?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"> <Button android:text="自定義登陸" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button5" android:onClick="login"/> </LinearLayout>
Layout(login_layout登陸窗口)界面:
<?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"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:hint="請(qǐng)輸入用戶名" android:id="@+id/et_username"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="請(qǐng)輸入密碼" android:id="@+id/et_password"/> </LinearLayout>
java功能實(shí)現(xiàn)代碼:
public class AlertDialogDemo extends AppCompatActivity { private EditText et_username,et_password; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alertdialog); } public void login(View v){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("登錄"); //通過(guò)布局填充器獲login_layout View view = getLayoutInflater().inflate(R.layout.login_layout,null); //獲取兩個(gè)文本編輯框(密碼這里不做登陸實(shí)現(xiàn),僅演示) final EditText et_username = (EditText) view.findViewById(R.id.et_username); final EditText et_password = (EditText) view.findViewById(R.id.et_password); builder.setView(view);//設(shè)置login_layout為對(duì)話提示框 builder.setCancelable(false);//設(shè)置為不可取消 //設(shè)置正面按鈕,并做事件處理 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String name = et_username.getText().toString().trim(); String pass = et_password.getText().toString().trim(); Toast.makeText(AlertDialogDemo.this,name + "正在登錄....",Toast.LENGTH_SHORT).show(); } }); //設(shè)置反面按鈕,并做事件處理 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(AlertDialogDemo.this,"取消登錄",Toast.LENGTH_SHORT).show(); } }); builder.show();//顯示Dialog對(duì)話框 } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 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 自定義AlertDialog對(duì)話框樣式
- Android對(duì)話框AlertDialog.Builder使用方法詳解
- android自定義AlertDialog對(duì)話框
- Android Alertdialog(實(shí)現(xiàn)警告對(duì)話框)
- Android開發(fā)之AlertDialog實(shí)現(xiàn)彈出對(duì)話框
相關(guān)文章
Android 同時(shí)setTag兩次保存多種值的示例代碼
這篇文章主要介紹了Android 同時(shí)setTag兩次保存多種值的示例代碼,需要的朋友可以參考下2017-02-02Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法
今天小編就為大家分享一篇Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程
最近在項(xiàng)目中要用到曲線圖,于是在網(wǎng)上找了很多很多,有AChartengine,MPAndroidChart,helloChart等等,我還用過(guò)基于html5的jsChart來(lái)做過(guò),不過(guò)最終還是選擇了MPAndroidChart來(lái)做本文介紹了Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程,需要的朋友可以參考下。2018-03-03Android判斷當(dāng)前棧頂Activity的包名代碼示例
這篇文章主要介紹了Android判斷當(dāng)前棧頂Activity的包名代碼示例,分享了相關(guān)代碼,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02解析:ClickOnce通過(guò)URL傳遞參數(shù) XXX.application?a=1
本篇文章是對(duì)ClickOnce通過(guò)URL傳遞參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android入門之ViewFlipper翻轉(zhuǎn)視圖的使用詳解
本篇給大家?guī)Я说氖荲iewFlipper,它是Android自帶的一個(gè)多頁(yè)面管理控件,且可以自動(dòng)播放!本篇我們我們會(huì)使用兩個(gè)例子:一個(gè)自動(dòng)播放首頁(yè)輪播頁(yè)一個(gè)手動(dòng)可左右滑動(dòng)道頁(yè)的輪播頁(yè)來(lái)說(shuō)透這個(gè)組件的使用,感興趣的可以了解一下2022-11-11