Android设置AlertDialog中按钮的可用(Enable)状态_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android设置AlertDialog中按钮的可用(Enable)状态

Android设置AlertDialog中按钮的可用(Enable)状态

 2014/12/3 16:11:34  Rossoneri.Du  程序员俱乐部  我要评论(0)
  • 摘要:弹出一个保存文件的对话框,要控制输入内容限制,同时内容为空时保存按钮不可用。直接上代码:LayoutInflatermInflater=LayoutInflater.from(mActivity);ViewsaveView=mInflater.inflate(R.layout.alert_dlg_save,null);//设置输入文件名的EditText属性finalEditTextfilename=(EditText)saveView.findViewById(R.id
  • 标签:android

弹出一个保存文件的对话框,要控制输入内容限制,同时内容为空时保存按钮不可用。直接上代码:

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

 

发表评论
用户名: 匿名