如何在edittextpreference对话框中设置null验证

时间:2013-08-19 06:40:40

标签: android alertdialog preferenceactivity edittextpreference

如何在edittextpreference对话框中设置null验证,这样如果它为null,则用户不能单击ok,并且应在对话框本身中显示一些消息。我一直试图找到它很长一段时间但没有成功......

3 个答案:

答案 0 :(得分: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已经告知的那样创建一个自定义对话框。

答案 1 :(得分:2)

我认为你在寻找的是这个。您正在使用预定义的PreferenceDialog(使用EditText)并想要检查Text是否为null。根据我的知识,这个案例中的“文本”是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;
              }
         }
   });

对于更高级的要求,我建议你实现自己的对话,并在其中,做任何你想做的事情。您可以通过在.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();

答案 2 :(得分:2)

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

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