Android实现对话框可以使用AlertDialog.Builder类,还可以自定义对话框。如果对话框有button.需要对其设置监听事件OnClickListener.
AlertDialog创建对话框有以下几个常用方法:
setTitle();//给对话框设置title;
setIcon();//给对话框设置图标;
setMessage();//设置对话框的提示信息;
setItems();//设置对话框要显示的一个List,一般用于显示几个命令时。
setSingleChoiceItems();//设置对话框显示一个单选的List
setMultiChoiceItems();//设置对话框显示一系统的复选框;
setPositiveButton();//给对话框添加"Yes"按钮;
setNegativeButton();//给对话框添加"No"按钮。
效果图:
代码:
1.res/values/string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">这里演示的是登录对话框,详细可登录http://www.80080088.com了解!</string> <string name="app_name">登录对话框</string> <string name="accountid">帐号:</string> <string name="password">密码:</string> <string name="login_title">登录提示</string> <string name="here_must_login">这里需要登录!</string> <string name="ok">确定</string> <string name="no">取消</string> <string name="exit">退出</string> <string name="login_tile">登录框</string> <string name="pls_waiting">请等侯...</string> <string name="logining">正在为你登录...</string> </resources>
2.res/layout/main.xml和login_dialog.xml
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
login_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="@string/accountid" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium"> </TextView> <EditText android:id="@+id/username" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:autoText="false" android:capitalize="none" android:gravity="fill_horizontal" android:textAppearance="?android:attr/textAppearanceMedium"> </EditText> <TextView android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="@string/password" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium"> </TextView> <EditText android:id="@+id/password" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:autoText="false" android:capitalize="none" android:gravity="fill_horizontal" android:password="true" android:textAppearance="?android:attr/textAppearanceMedium"> </EditText> </LinearLayout>
3.Activity类
package com.cottsoft.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class A012Activity extends Activity {
private ProgressDialog m_Dialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog dialog = new AlertDialog.Builder(A012Activity.this)
.setTitle(getResources().getString(R.string.login_title))
.setMessage(getResources().getString(R.string.here_must_login))
.setPositiveButton(getResources().getString(R.string.ok),new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
LayoutInflater factory = LayoutInflater.from(A012Activity.this);
final View DialogView = factory.inflate(R.layout.login_dialog, null);
AlertDialog dlg = new AlertDialog.Builder(A012Activity.this)
.setTitle(getResources().getString(R.string.login_title))
.setView(DialogView)
.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
m_Dialog = ProgressDialog.show(A012Activity.this, getResources().getString(R.string.pls_waiting), getResources().getString(R.string.logining),true);
new Thread(){
public void run(){
try{}catch(Exception ex){
ex.printStackTrace();
}finally{
m_Dialog.dismiss();
}
}
}.start();
}
})
.setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
A012Activity.this.finish();
}
})
.create();
dlg.show();
}
}).setNeutralButton(getResources().getString(R.string.exit), new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
A012Activity.this.finish();
}
}).create();
dialog.show();
}
}