Android Dev Notes - 11 - 关于Dialog(Con't)

本文介绍了一个自定义配置对话框的实现方法,包括错误提示和用户输入验证。该对话框用于获取用户输入的商户ID和刷新间隔,并通过检查确保输入有效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

显示dialog,带错误信息,自己写的,作为我的Dialog的实例参考


private void showConfigDialog(int shopId, long interval, int error)
	{		
		configView = getLayoutInflater().inflate(R.layout.configdlg, null);
		TextView edtInterval = (TextView)configView.findViewById(R.id.edtInterval);
		TextView edtShopId = ((TextView)configView.findViewById(R.id.edtShopId));
		if(error != 0)
		{
			if((error & 0x1) != 0){
				edtShopId.setText(String.valueOf(shopId));
				edtShopId.setError("商户ID输入错误");
				edtShopId.requestFocus();
			}
			if((error & 0x2) != 0){
				edtInterval.setText(String.valueOf(interval));
				edtInterval.setError("刷新间隔输入错误");
				edtInterval.requestFocus();
			}
		}		
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(configView);
		builder.setTitle("参数配置");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int	which) {
				int error = 0;
				TextView edtInterval = (TextView)configView.findViewById(R.id.edtInterval);
				/**输入的interval的单位为秒*/
				long interval = Long.parseLong(edtInterval.getText().toString());
				TextView edtShopId = (TextView)configView.findViewById(R.id.edtShopId);
				int shopId = Integer.parseInt((edtShopId.getText().toString()));	
				if(shopId == 0) error = error|0x1;			
				if(interval <= 0) error = error|0x2;
				if(error == 0)
				{
					config.setConfig(interval*1000, shopId);
					if(refreshThread == null||refreshThread.isAlive() == false){
						refreshThread = new RerefreshThread();
						refreshThread.start();
					}
				}
				else
				{
					showConfigDialog(shopId, interval, error);
				}
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		
		AlertDialog dlg = builder.create();
		dlg.show();
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值