Android Dialog的使用

Android中实现对话框可以使用AlertDialog.Builder类,还可以自定义对话框.如果对话框设置了按钮就需要对其设置事件监听OnClickListener.
下面将列出一个示例:
<?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:id="@+id/t_username" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="帐号"
		android:layout_marginLeft="20dip" />
	<EditText android:id="@+id/e_username" android:hint="输入帐号"
		android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
		android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText>
	<TextView android:id="@+id/t_password" android:layout_width="wrap_content"
		android:layout_marginLeft="20dip" android:layout_height="wrap_content"
		android:text="密码"></TextView>
	<EditText android:id="@+id/e_password" android:hint="输入密码"
		android:password="true" android:layout_marginLeft="20dip"
		android:layout_marginRight="20dip" android:layout_width="fill_parent"
		android:layout_height="wrap_content"></EditText>
</LinearLayout>


package com.Aina.Android;

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

public class Test_Dialog extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	String[] str = { "111", "222", "333", "444" };
	TextView tv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		tv = (TextView) this.findViewById(R.id.TextView);
		Dialog dialog = new AlertDialog.Builder(this).setTitle("对话框标题")
				.setIcon(R.drawable.icon).setMessage("登陆对话框")
				// .setItems(str, Test_Dialog.this)// 设置对话框要显示的一个list
				// .setSingleChoiceItems(str, 0, Test_Dialog.this)//
				// 设置对话框显示一个单选的list
				.setPositiveButton("确定", Test_Dialog.this)
//				.setNegativeButton("取消", Test_Dialog.this)
				.setNeutralButton("退出",Test_Dialog.this)
				.create();
		dialog.show();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		if (which == Dialog.BUTTON_POSITIVE) {
			// tv.setText("点击了确定按钮");
			this.login();
		} else if (which == Dialog.BUTTON_NEUTRAL) {
			this.finish();
		} else {
			tv.setText("点击了" + str[which]);
		}
	}

	public void login() {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.dialog, null);
		Dialog dialog = new AlertDialog.Builder(this).setTitle("登陆").setView(
				view).setPositiveButton("登陆", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				final ProgressDialog pdialog = ProgressDialog.show(Test_Dialog.this, "Login", "登陆中...", true);
				new Thread(){
					public void run(){
						try {
							Thread.sleep(3000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}finally{
							pdialog.dismiss();
						}
					}
				}.start();
				
				
			}

		}).setNegativeButton("退出", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Test_Dialog.this.finish();
			}

		}).create();
		dialog.show();
	}
}

你可能感兴趣的:(thread,android,xml,OS)