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(); } }
单击显示按钮:
注意按钮的顺序,单击“取消”
单击“忽略”
单击“确定”: