package com.lanxiang.myapps; import android.app.AlertDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } private void initView() { } private void initData() { } public void click1(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示"); builder.setMessage("是否删除当前选择项?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "啊啊啊啊啊啊啊啊啊啊", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", null); builder.show(); } public void click2(View v) { final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选泽你想免费的商品"); final String[] items = {"别墅房","美女主播","黄瓜"}; /** * 单选对话框 */ builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "我就知道你会选我的!", Toast.LENGTH_SHORT).show(); dialogInterface.dismiss(); } }); //设置用户不可点击除对话框以外的地方,导致对话框消失 builder.setCancelable(false); builder.setNegativeButton("取消",null); builder.show(); } public void click3(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择你喜欢的美女类型"); String[] items = {"少女","少妇","成熟","小鸟依人","霸道总裁","活泼性感"}; boolean[] checkedItems = {true, true, false, false,false,false}; builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i, boolean b) { Toast.makeText(MainActivity.this, "我就知道你会选我的!", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消选择", null); builder.show(); } }