android Dialog

新手写给自己看


@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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值