AlterDialog类的构造方法被声明成protected方法,因此,不能直接使用new关键字来创造对象实例。每个对话框最多自能添加三个Button,方法分别是:setNegativeButton,setNeutralButton,setPositiveButton。并且按钮出现顺序同上,如果相同的方法调用多次,以最后一次为准。具体见代码:
package me.chendd.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AndroidDialogActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener( this);
}
@ Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder( this).setIcon(R.drawable.ic_launcher).
setTitle( "是否删除文件").setPositiveButton( "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
new AlertDialog.Builder(AndroidDialogActivity. this).setMessage(
"文件已被删除。").create().show();
}
}).setNegativeButton( "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
new AlertDialog.Builder(AndroidDialogActivity. this).setMessage(
"你选择了取消按钮,文件未被删除。").create().show();
}
}).setNeutralButton( "忽略",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
new AlertDialog.Builder(AndroidDialogActivity. this).setMessage(
"你选择了忽略按钮").create().show();
}
}).show();
}
}
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AndroidDialogActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener( this);
}
@ Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder( this).setIcon(R.drawable.ic_launcher).
setTitle( "是否删除文件").setPositiveButton( "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
new AlertDialog.Builder(AndroidDialogActivity. this).setMessage(
"文件已被删除。").create().show();
}
}).setNegativeButton( "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
new AlertDialog.Builder(AndroidDialogActivity. this).setMessage(
"你选择了取消按钮,文件未被删除。").create().show();
}
}).setNeutralButton( "忽略",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
new AlertDialog.Builder(AndroidDialogActivity. this).setMessage(
"你选择了忽略按钮").create().show();
}
}).show();
}
}
单击显示按钮:
![](https://i-blog.csdnimg.cn/blog_migrate/ec884c1351120538d16ee47d9101bcdc.png)
注意按钮的顺序,单击“取消”
![](https://i-blog.csdnimg.cn/blog_migrate/3ea39fc20e03cf7a44bec928899b5595.png)
单击“忽略”
![](https://i-blog.csdnimg.cn/blog_migrate/f2b1f9ae6b4608864de9671bb35d3059.png)
单击“确定”:
![](https://i-blog.csdnimg.cn/blog_migrate/d9c7c79d6ba397afda3240c6887507f6.png)
转载于:https://blog.51cto.com/aiguozhe/914421