AlertDialog.Builder setPositiveButton 点击时不关闭dialog

2022-07-04 11:12:21 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

AlertDialog.Builder setPositiveButton 与 setNegativeButton 点击时都会自动关闭 dialog,但是 文本控制 不想其关闭 两种方法

①:

LayoutInflater inflater = LayoutInflater.from(mActivity);

View view = inflater.inflate(R.layout.dialog_chang_script_id, null); AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); builder.setTitle(mActivity.getString(R.string.script_btn_change_orider_id_titel)); builder.setView(view); final EditText et = (EditText) view.findViewById(R.id.et_script_or_id); builder.setNegativeButton(mActivity.getString(R.string.script_btn_cancel), null); builder.setPositiveButton(mActivity.getString(R.string.script_btn_yes), null); builder.create(); final AlertDialog dialog = builder.create(); dialog.show(); dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(TextUtils.isEmpty(et.getText())){ viewInterface.toast(mActivity.getString(R.string.script_orider_id_can_not_null)); et.setError(mActivity.getString(R.string.script_orider_id_can_not_null)); }else { SPrefHookUtil.putSettingStr(mActivity, SPrefHookUtil.KEY_SETTING_ORDER_ID, et.getText().toString()); getIsScriptExist(getOriderId()); dialog.dismiss(); } } });

}

② 反射

LayoutInflater layoutInflater = LayoutInflater.from(context); View dialogView = layoutInflater.inflate(R.layout.dialog_edit_text,null); final EditText et = (EditText) dialogView.findViewById(R.id.et_dialog_text); Builder builder = new AlertDialog.Builder(context); builder.setTitle(context.getString(R.string.liucun_dialog_set_total_title)); builder.setView(dialogView); builder.setNegativeButton(context.getString(R.string.liucun_dialog_btn_cancel), null); builder.setPositiveButton(context.getString(R.string.liucun_dialog_btn_ok),new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String count = et.getText().toString(); Logger.i(“==count==” count); if (TextUtils.isEmpty(count)) { et.setError(context.getString(R.string.liucun_tip_total_count_not_null)); viewInterface.toast(context.getString(R.string.liucun_tip_total_count_not_null)); try { Field field = dialog.getClass().getSuperclass().getDeclaredField(“mShowing”); field.setAccessible(true); field.set(dialog, false); Logger.i(“==field==” field); } catch (Exception e) { e.printStackTrace(); Logger.i(“==field=Exception=” e); } } else { int integer = Integer.parseInt(count); boolean result = SPrefHookUtil.putSettingInt(context,SPrefHookUtil.KEY_SETTING_RUN_TIMES,integer); if (result) { viewInterface.toast(context.getString(R.string.liucun_tip_total_count_set_ok)); int totalCount = SPrefHookUtil.getSettingInt(context,SPrefHookUtil.KEY_SETTING_RUN_TIMES,SPrefHookUtil.D_SETTING_RUN_TIMES); viewInterface.setTotalCountText(totalCount); } else { viewInterface.toast(context.getString(R.string.liucun_tip_total_count_set_err)); } dialog.dismiss(); } } }); final AlertDialog dialog = builder.create();

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149263.html原文链接:https://javaforall.cn

0 人点赞