編輯:關於Android編程
在Android開發中,我們經常會需要在Android界面上彈出一些對話框,比如詢問用戶或者讓用戶選擇。這些功能我們叫它Android Dialog對話框,AlertDialog實現方法為建造者模式。AlertDialog中定義的一些對話框往往無法滿足我們關於對話框的需求,這時我們就需要通過自定義對話框VIEW來實現需求,這裡我自定義一個登陸的提示對話框,效果圖顯示如下:

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功能實現代碼:
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);
//獲取兩個文本編輯框(密碼這裡不做登陸實現,僅演示)
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);//設置login_layout為對話提示框
builder.setCancelable(false);//設置為不可取消
//設置正面按鈕,並做事件處理
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();
}
});
//設置反面按鈕,並做事件處理
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogDemo.this,"取消登錄",Toast.LENGTH_SHORT).show();
}
});
builder.show();//顯示Dialog對話框
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android開發之使用GridView展示圖片的方法
本文實例講述了Android使用GridView展示圖片的方法。分享給大家供大家參考,具體如下:今天說說GridView的使用。所謂GvidView翻譯過來就是網格布局:
Android Service
1. Service和IntentService區別Servicehttp://android.xsoftlab.net/reference/android/app/Se
Android中ViewFlipper的使用及設置動畫效果實例詳解
本文實例講述了Android中ViewFlipper的使用及設置動畫效果。分享給大家供大家參考,具體如下:說到左右滑動,其實實現左右滑動的方式很多,有ViewPaer,自
分析Android程序之破解第一個程序
破解Android程序通常的方法是將apk文件利用ApkTool反編譯,生成Smali格式的反匯編代碼,然後閱讀Smali文件的代碼來理解程序的運行機制,找到