string.xml
TestBase 城市 北京 是否选择提示框 列表选择提示框
main.xml
activity.java
package com.mytest.testbase; 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.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView textview_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview_result = (TextView) this.findViewById(R.id.tv_result); } /** * 是否选择提示框 * * @param view */ public void YesOrNoOnClick(View view) { Builder builder = new AlertDialog.Builder(this); builder.setMessage("确认删除吗?").setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Toast.makeText(getApplicationContext(), "删除成功", // Toast.LENGTH_SHORT).show(); textview_result.setText("删除成功"); } }).setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Toast.makeText(getApplicationContext(), "删除失败", // Toast.LENGTH_SHORT).show(); textview_result.setText("删除失败"); } }); AlertDialog dialog = builder.create(); dialog.show(); } /** * 列表项选择提示框 * * @param view */ public void ListItemOnClick(View view) { Builder builder = new AlertDialog.Builder(this); final String[] cities = { "广州", "上海", "北京", "香港", "澳门" }; builder.setTitle("选择城市").setItems(cities, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { textview_result.setText("选择的城市是" + cities[which]); } }); AlertDialog dialog = builder.create(); dialog.show(); } }