1.自定义对话框
下面我们要自定义的对话框,包括两个TextView和两个EditText,所以需要在布局文件中定义这个对话框界面,实现如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/username"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="账号"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText
android:id="@+id/username"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="@+id/password"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="密码"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:password="true"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
2. 使用AlertDialog来创建对话框
使用AlertDialog来创建对话框需要知道下面几个方法:
setTitle():设置标题
setIcon():设置图标
setMessage():设置对话框的提示信息
setItems():设置对话框要显示的一个list,一般用于几个命令时
setSingleChoiceItems():设置对话框要显示一个单选的List
setMultiChoiceItems():用来设置对话框显示的一系列的复选框
setPositiveButton():给对话框添加yes按钮
setNegativeButton():给对话框添加No按钮。
package com.example.mydialog;
import android.os.Bundle;
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.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
ProgressDialog m_Dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Dialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("登陆提示")
.setMessage("这里需要登陆")
.setPositiveButton("确定", new MyDialogClickListener())
.setNeutralButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
MainActivity.this.finish();
}
}).create();
dialog.show();
}
class MyDialogClickListener implements DialogInterface.OnClickListener{
@Override
public void onClick(DialogInterface arg0, int arg1) {
//点击确定之后转向登陆框
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
//得到自定义对话框
final View DialogView = factory.inflate(R.layout.dialog, null);
//创建对话框
AlertDialog dlg = new AlertDialog.Builder(MainActivity.this)
.setTitle("登陆框")
.setView(DialogView)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
//输入完成后,点击"确定"开始登陆
m_Dialog = ProgressDialog.show(MainActivity.this, "请等待...","正在为您登陆...",true);
new Thread(){
public void run(){
try{
sleep(3000);
}catch(Exception e){
e.printStackTrace();
}finally{
//登陆结束,取消m_Dialog对话框
m_Dialog.dismiss();
}
}
}.start();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
//点击"取消"按钮之后退出程序
MainActivity.this.finish();
}
}).create();
dlg.show();
}
}
}