5

如何在 edittextpreference 对话框中设置 null 验证,以便如果它为 null,则用户不应单击确定,并且应在对话框本身中显示一些消息。找了好久都没成功。。。。。

4

3 回答 3

6
edttxtpref = (EditTextPreference) getPreferenceScreen().findPreference(
            "edttxtkey");
    edttxtpref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(
                android.preference.Preference preference, Object newValue) {
            if (newValue.toString().trim().equals("")) {

                Toast.makeText(getActivity(), "Username can not be empty",
                        Toast.LENGTH_LONG).show();

                return false;
            }
            return true;
        }
    });

这样验证就完成了,如果我们想在对话框本身中显示消息,那么必须创建一个自定义对话框,正如 Phil 已经告知的那样。

于 2013-08-19T13:21:04.343 回答
2

我想你正在寻找的是这个。您正在使用预定义的 PreferenceDialog(带有 EditText)并想要检查 Text 是否为空。据我所知,这种情况下的“文本”是 changedPreference,因此您可以这样做:

只需使用onPreferenceChangedListener 即可

yourPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
         @Override
         public boolean onPreferenceChange(Preference preference, Object changedValue) {

              if(changedValue == null) {
                    // handle it being null

                    return false;
              } else {

                    return true;
              }
         }
   });

对于更高级的要求,我建议您实现自己的 Dialog并在其中做任何您想做的事情。您可以通过在 .xml 中定义首选项列表条目来实现这一点,然后在单击它时生成对话框。

Preference yourCustomPref = (Preference) findPreference("yourPref");
yourCustomPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {

            // spawn your dialog here
            return true;
        }
    });

这就是您可以实现自定义 EditText 对话框的方式:

public Builder buildDialog(final Context c) {

        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setTitle("EditText Dialog");
        builder.setMessage("Enter text:");

        LinearLayout llV = new LinearLayout(c);
        llV.setOrientation(1); // 1 = vertical

        final EditText patName = new EditText(c);
        patName.setHint("Enter text...");

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f);   
        lp.bottomMargin = 20;
        lp.rightMargin = 30;
        lp.leftMargin = 15;

        patName.setLayoutParams(lp);

        llV.addView(patName);

        builder.setView(llV);

        builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                if(patName.getText().toString().length() > 0) {

                } else {

                }       
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });

        return builder;
    }

然后这样称呼它:

buildDialog(yourcontext).show();
于 2013-08-19T06:47:31.387 回答
2

当edittext为空时,确定按钮将被禁用,一旦输入文本,它将被启用::

public class CustomEditTextPreference extends EditTextPreference implements
        OnClickListener {


        public CustomEditTextPreference(Context ctx, AttributeSet attrs, int defStyle)
        {
            super(ctx, attrs, defStyle);        
        }

        public CustomEditTextPreference(Context ctx, AttributeSet attrs)
        {
            super(ctx, attrs);                
        }

        private class EditTextWatcher implements TextWatcher
        {    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count){}

            @Override
            public void beforeTextChanged(CharSequence s, int start, int before, int count){}

            @Override
            public void afterTextChanged(Editable s)
            {        
                onEditTextChanged();
            }
        }
        EditTextWatcher m_watcher = new EditTextWatcher();

        /**
         * Return true in order to enable positive button or false to disable it.
         */
        protected boolean onCheckValue(String value)
        {        
            if (value.trim().equals(""))
            {
                return false;
            }
            return true;
        }

        protected void onEditTextChanged()
        {
            boolean enable = onCheckValue(getEditText().getText().toString());
            Dialog dlg = getDialog();
            if(dlg instanceof AlertDialog)
            {
                AlertDialog alertDlg = (AlertDialog)dlg;
                Button btn = alertDlg.getButton(AlertDialog.BUTTON_POSITIVE);
                btn.setEnabled(enable);                
            }
        }

        @Override
        protected void showDialog(Bundle state)
        {
            super.showDialog(state);

            getEditText().removeTextChangedListener(m_watcher);
            getEditText().addTextChangedListener(m_watcher);
            onEditTextChanged();
        }    
    }
于 2013-08-21T07:11:07.773 回答