超简单的自定义AlertDialog弹出键盘可以输入的

超简单的自定义AlertDialog弹出键盘可以输入的

demo下载


非常简单,没写什么注释,相信大家一看就明白了

private void showAlertDialog() {
		final AlertDialog dialog = new AlertDialog.Builder(this).create();
		dialog.setView(LayoutInflater.from(this).inflate(R.layout.alert_dialog, null));
		dialog.show();
		dialog.getWindow().setContentView(R.layout.alert_dialog);
		Button btnPositive = (Button) dialog.findViewById(R.id.btn_add);
		Button btnNegative = (Button) dialog.findViewById(R.id.btn_cancel);
		final EditText etContent = (EditText) dialog.findViewById(R.id.et_content);
		btnPositive.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String str = etContent.getText().toString();
				if (isNullEmptyBlank(str)) {
					etContent.setError("输入内如不能为空");
				} else {
					dialog.dismiss();
					Toast.makeText(MainActivity.this, etContent.getText().toString(), Toast.LENGTH_LONG).show();
				}
			}
		});
		btnNegative.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});
	}

	private static boolean isNullEmptyBlank(String str) {
		if (str == null || "".equals(str) || "".equals(str.trim()))
			return true;
		return false;
	}
demo下载

你可能感兴趣的:(android,自定义,AlertDialog)