1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private boolean[] checkItems;
private AlertDialog alert = null; private AlertDialog.Builder builder = null;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
findViewById(R.id.btn1).setOnClickListener(this); findViewById(R.id.btn2).setOnClickListener(this); findViewById(R.id.btn3).setOnClickListener(this); findViewById(R.id.btn4).setOnClickListener(this); }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn1: alert = null; builder = new AlertDialog.Builder(MainActivity.this); alert = builder.setIcon(R.mipmap.ic_launcher_round) .setTitle("Tip") .setMessage("你收到了一首特别诗,要看吗?") .setNegativeButton("不要", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "Canceled.", Toast.LENGTH_SHORT).show(); } }) .setPositiveButton("当然", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "\"Get out of my head.Get out of my head.Get out of my head.Get out of my head.\nGet\nout\nof\nmy\nhead\"", Toast.LENGTH_SHORT).show(); } }) .setNeutralButton("忽略", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "Nothing happened.", Toast.LENGTH_SHORT).show(); } }).create(); alert.show(); break; case R.id.btn2: final String[] members = new String[]{"Sayori", "Natsuki", "Yuri", "Monika"}; alert = null; builder = new AlertDialog.Builder(MainActivity.this); alert = builder.setIcon(R.mipmap.ic_launcher_round) .setTitle("要把诗给谁看呢") .setItems(members, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你把诗给了" + members[which], Toast.LENGTH_SHORT).show(); } }).create(); alert.show(); break; case R.id.btn3: final String[] fruits = new String[]{"Monika", "M nika", "M?nIkA", "mONiKA", " o ika"}; alert = null; builder = new AlertDialog.Builder(MainActivity.this); alert = builder.setIcon(R.mipmap.ic_launcher_round) .setTitle("周日你将会和谁进行社团活动呢") .setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你选择了" + fruits[which], Toast.LENGTH_SHORT).show(); } }).create(); alert.show(); break; case R.id.btn4: final String[] menu = new String[]{"Monika", "Sayori", "Natsuki", "Yuri"}; checkItems = new boolean[]{false, false, false, false}; alert = null; builder = new AlertDialog.Builder(MainActivity.this); alert = builder.setIcon(R.mipmap.ic_launcher_round) .setTitle("来吧,像个大人一样做出选择") .setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkItems[which] = isChecked; } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result = ""; for (int i = 0; i < checkItems.length; i++) { if (checkItems[i]) result += menu[i] + " "; } Toast.makeText(getApplicationContext(), "这些我全都要 " + result, Toast.LENGTH_SHORT).show(); } }) .create(); alert.show(); break; } } }
|