对话框控件:最多3个按钮。

mainActivity.java

package com.sxt.day05_09;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListener();
} private void setListener() {
setStandardDialogClickListener();
setItemsDialogClickListener();
setCustomDialogClickListener();
} //自定义对话框
private void setCustomDialogClickListener() {
findViewById(R.id.btnCustomDialog).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//将custom_dialog.xml解析为一个View类型的java对象
View layout = View.inflate(MainActivity.this, R.layout.custom_dialog, null);
final EditText etAddrss=(EditText) layout.findViewById(R.id.etAddress);
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);//Builder是内部类
builder.setTitle("输入地址的对话框")//setTitle()返回builder对象(builder对象的地址),所以可以继续用点调用其他方法,
.setView(layout)//添加自定义布局,替换掉setMessage()
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String address=etAddrss.getText().toString();//内部类调用外部的方法的变量要是final类型
Log.i("main",address);
}
});
AlertDialog dialog = builder.create();//create最终创建对话框
dialog.show();//显示出来
}
});
} //列表类型的对话框
private void setItemsDialogClickListener() {
findViewById(R.id.btnItemsDialog).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//真正创建对话框不是AlertDialog,而是这个类的内部类Builder,
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setItems(new String[]{"增加数据","删除数据","修改数据"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {//dialog是当前操作的AlertDialog对象,
switch (which) {
case 0:
Toast.makeText(MainActivity.this, "执行增加数据的操作", 3000).show();
break;
case 1:
Toast.makeText(MainActivity.this, "执行删除数据的操作", 3000).show();
break;
case 2:
Toast.makeText(MainActivity.this, "执行修改数据的操作", 3000).show();
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
} //标准对话框
private void setStandardDialogClickListener() {
findViewById(R.id.btnStandardDialog).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建对话框的Builder对象,Builder是AlertDialog的内部类
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("退出考试窗口")
.setMessage("退出考试吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//setPositiveButton创建肯定的按钮,按钮文本是"确定",点击按钮的监听器是DialogInterface.OnClickListener
//OnClickListener是DialogInterface的内部接口,
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("main","退出考试");
}
}).setNegativeButton("放弃", new DialogInterface.OnClickListener() {////setNegativeButton创建否定的按钮,按钮的文本和监听器
//如果不添加监听器,则仅仅关闭对话框没有动作。
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("main","继续考试");
}
});
AlertDialog dialog = builder.create();//create最终创建对话框
dialog.show();//显示出来
}
});
} }

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btnStandardDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标准对话框" />
<Button
android:id="@+id/btnItemsDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表对话框" />
<Button
android:id="@+id/btnCustomDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义对话框" /> </LinearLayout>
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入地址"/>
<EditText
android:id="@+id/etAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
05-11 20:47