Android:自定义对话框

项目名称:dev.sky.android.CustomDialog

自定义一个登录对话框:

Xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content">
<TextView
android:text="用户名"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:textSize
="20sp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:id
="@+id/user"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:text
="密码"
android:textSize
="20sp"
/>
<EditText
android:id="@+id/password"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:password
="true"
/>
</LinearLayout>

  CutomDialogActivity.java

package dev.sky.android.custom_dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

publicclass CustomDialogActivity extends Activity {
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

showDialog(
0);
}

@Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder
=new AlertDialog.Builder(this);
AlertDialog customDialog;

LayoutInflater inflater
= getLayoutInflater();
View layout
= inflater.inflate(R.layout.custom, null);
builder.setTitle(
"登录");
builder.setView(layout);
builder.setPositiveButton(
"登录", new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface arg0, int arg1) {
mProgress
= ProgressDialog.show(CustomDialogActivity.this,"" , "正在登录");

// 新线程,睡眠3秒然后关闭ProgressDialog
new Thread() {
publicvoid run() {
try {
sleep(
3000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally {
mProgress.dismiss();
}
}
}.start();
}
});
builder.setNegativeButton(
"取消", new DialogInterface.OnClickListener() {

@Override
publicvoid onClick(DialogInterface arg0, int arg1) {
CustomDialogActivity.
this.finish();
}
});
customDialog
= builder.create();

return customDialog;
}
private ProgressDialog mProgress;
}

  

原文地址:https://www.cnblogs.com/skyhacker/p/2138592.html