ANDROID中自定義對話框AlertDialog使用示例
在Android開發(fā)中,我們經(jīng)常會需要在Android界面上彈出一些對話框,比如詢問用戶或者讓用戶選擇。這些功能我們叫它Android Dialog對話框,AlertDialog實現(xiàn)方法為建造者模式。AlertDialog中定義的一些對話框往往無法滿足我們關(guān)于對話框的需求,這時我們就需要通過自定義對話框VIEW來實現(xiàn)需求,這里我自定義一個登陸的提示對話框,效果圖顯示如下:

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="請輸入用戶名"
android:id="@+id/et_username"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="請輸入密碼"
android:id="@+id/et_password"/>
</LinearLayout>
java功能實現(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("登錄");
//通過布局填充器獲login_layout
View view = getLayoutInflater().inflate(R.layout.login_layout,null);
//獲取兩個文本編輯框(密碼這里不做登陸實現(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為對話提示框
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對話框
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)點擊AlertDialog上按鈕時不關(guān)閉對話框的方法
- Android中AlertDialog各種對話框的用法實例詳解
- Android使用AlertDialog實現(xiàn)的信息列表單選、多選對話框功能
- Android中AlertDialog 點擊按鈕后不關(guān)閉對話框的功能
- Android修改源碼解決Alertdialog觸摸對話框邊緣消失的問題
- Android 自定義AlertDialog對話框樣式
- Android對話框AlertDialog.Builder使用方法詳解
- android自定義AlertDialog對話框
- Android Alertdialog(實現(xiàn)警告對話框)
- Android開發(fā)之AlertDialog實現(xiàn)彈出對話框
相關(guān)文章
Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法
今天小編就為大家分享一篇Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程
最近在項目中要用到曲線圖,于是在網(wǎng)上找了很多很多,有AChartengine,MPAndroidChart,helloChart等等,我還用過基于html5的jsChart來做過,不過最終還是選擇了MPAndroidChart來做本文介紹了Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程,需要的朋友可以參考下。2018-03-03
Android判斷當(dāng)前棧頂Activity的包名代碼示例
這篇文章主要介紹了Android判斷當(dāng)前棧頂Activity的包名代碼示例,分享了相關(guān)代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
解析:ClickOnce通過URL傳遞參數(shù) XXX.application?a=1
本篇文章是對ClickOnce通過URL傳遞參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android入門之ViewFlipper翻轉(zhuǎn)視圖的使用詳解
本篇給大家?guī)Я说氖荲iewFlipper,它是Android自帶的一個多頁面管理控件,且可以自動播放!本篇我們我們會使用兩個例子:一個自動播放首頁輪播頁一個手動可左右滑動道頁的輪播頁來說透這個組件的使用,感興趣的可以了解一下2022-11-11

