Android学习历程-AlerDialog
package com.example.testcontextmenu; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TestAlertDialog extends Activity { private TextView myTextView; private Button myButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTextView =(TextView)findViewById(R.id.myTextView); myButton = (Button)findViewById(R.id.myButton); final AlertDialog.Builder builder = new AlertDialog.Builder(this); myButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //设置显示信息 builder.setMessage("真的要删除该记录吗?").setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 删除成功 myTextView.setText("删除成功!"); } }).setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 取消删除 myTextView.setText("取消删除!"); } }); AlertDialog a = builder.create(); a.show(); } }); } }