新手写给自己看
@Override public void onClick(View v) { switch (v.getId()) { case R.id.tv1: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("确认退出吗?"); builder.setTitle("提示"); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); break; case R.id.tv2: View view = LayoutInflater.from(this).inflate(R.layout.view1, null); final EditText ed = (EditText) view.findViewById(R.id.ed); new AlertDialog.Builder(this).setTitle("请输入").setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String str = ed.getText().toString().trim(); Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show(); dialog.dismiss(); } }) .setNegativeButton("取消", null).show(); break; case R.id.tv3: new AlertDialog.Builder(this).setTitle("复选框") .setMultiChoiceItems(new String[]{"Item1", "Item2"}, null, null) .setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); break; case R.id.tv4: new AlertDialog.Builder(this).setTitle("单选框") .setSingleChoiceItems(new String[]{"Item1", "Item2"}, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .setNegativeButton("取消", null).show(); break; case R.id.tv5: ProgressDialog dialog = new ProgressDialog(this); dialog.setMessage("正在加载中..."); dialog.setIndeterminate(true); dialog.setCancelable(true); dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { //取消的监听 } }); dialog.show(); break; case R.id.tv6: Calendar c = Calendar.getInstance(); DatePickerDialog dialog1 = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) { // searchstarttime.setText(year + "年" + (month + 1) + "月" + dayOfMonth + "日"); Toast.makeText(MainActivity.this, year + "年" + (month + 1) + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show(); } }, c.get(Calendar.YEAR), // 传入年份 c.get(Calendar.MONTH), // 传入月份 c.get(Calendar.DAY_OF_MONTH) // 传入天数 ); dialog1.setTitle("选择日期"); dialog1.show(); break; case R.id.tv7: break; } }