首页 > 代码库 > Android设置AlertDialog中按钮的可用(Enable)状态
Android设置AlertDialog中按钮的可用(Enable)状态
弹出一个保存文件的对话框,要控制输入内容限制,同时内容为空时保存按钮不可用。直接上代码:
LayoutInflater mInflater = LayoutInflater.from(mActivity);View saveView = mInflater.inflate(R.layout.alert_dlg_save, null);// 设置输入文件名的EditText属性final EditText filename = (EditText) saveView.findViewById(R.id.dlg_save_input_filename);filename.setFilters(new InputFilter[] {new InputFilter.LengthFilter(255), new SpecialFilter() });filename.setText(mStrAsName);TextView defaultPath = (TextView) saveView.findViewById(R.id.dlg_save_default_path);defaultPath.setText(DataStruct.gFloder + "/");AlertDialog.Builder builder = new AlertDialog.Builder(mActivity, AlertDialog.THEME_HOLO_LIGHT);builder.setTitle(R.string.maker_menu_save);builder.setIcon(R.drawable.icon_small);builder.setView(saveView);builder.setPositiveButton(R.string.maker_menu_save, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { // TODO Auto-generated method stub String name = filename.getText().toString().trim(); name += ".astp"; String namePath = DataStruct.gFloder + "/" + name; if (saveFile(namePath)) { mStrSavedPath = namePath; mBtnSave.setEnabled(false); } else { ToastFailedSave(); } }});builder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { // TODO Auto-generated method stub } });Dialog dialog = builder.create();dialog.show();// 获取PositiveButton 重点在这里final Button btn = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);// 输入不能为空filename.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start,int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start,int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub String name = s.toString().trim(); if (name.isEmpty()) btn.setEnabled(false); else btn.setEnabled(true); }});
Android设置AlertDialog中按钮的可用(Enable)状态
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。