alertdialog_act.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="vertical"> <button android:id="@+id/alertDialog_btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:οnclick="alertDialog_ClickBtn" android:text="alertDialog_btn1"> </button><button android:id="@+id/alertDialog_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:οnclick="alertDialog_ClickBtn" android:text="alertDialog_btn2"> </button></linearlayout> alertdialog.xml文件 <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <edittext android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginleft="20dip" android:layout_marginright="20dip" android:textappearance="?android:attr/textAppearanceMedium" android:id="@+id/edtInput"> </edittext></linearlayout> AlertDialog_Act.java文件 /** <activity android:name="com.AlertDialog.AlertDialog_Act" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"> <category android:name="android.intent.category.LAUNCHER"> </category></action></intent-filter> </activity> */ import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; public class AlertDialog_Act extends Activity { private final static String TAG="AlertDialog_Act"; private Button alertDialog_btn1; private Button alertDialog_btn2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.alertdialog_act); alertDialog_btn1 = (Button)findViewById(R.id.alertDialog_btn1); alertDialog_btn2 = (Button)findViewById(R.id.alertDialog_btn2); } public void alertDialog_ClickBtn(View view){ if(view.equals(alertDialog_btn1)){ showDialog_Layout(); }else if(view.equals(alertDialog_btn2)){ showDialog(); } } //显示基于Layout的AlertDialog private void showDialog_Layout() { LayoutInflater inflater = LayoutInflater.from(AlertDialog_Act.this); final View textEntryView = inflater.inflate( R.layout.alertdialog, null); final EditText edtInput=(EditText)textEntryView.findViewById(R.id.edtInput); final AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialog_Act.this); builder.setCancelable(false); // builder.setIcon(R.drawable.icon); builder.setTitle("Title"); builder.setView(textEntryView); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle(edtInput.getText()); Log.i(TAG, "input="+edtInput.getText()); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle("no input"); Log.i(TAG, "input=null"); } }); builder.show(); } private void showDialog() { AlertDialog.Builder adb = new AlertDialog.Builder(AlertDialog_Act.this); adb.setTitle("退出"); adb.setMessage("是否退出软件?"); adb.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub AlertDialog_Act.this.finish(); } }); adb.setNeutralButton("中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //do nothing } }); adb.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //not exit } }); adb.show(); } }