Android設定AlertDialog中按鈕的可用(Enable)狀態,androidalertdialog

來源:互聯網
上載者:User

Android設定AlertDialog中按鈕的可用(Enable)狀態,androidalertdialog

彈出一個儲存檔案的對話方塊,要控制輸入內容限制,同時內容為空白時儲存按鈕不可用。直接上代碼:

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.