首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何验证输入字段并防止对话框消失

如何验证输入字段并防止对话框消失
EN

Stack Overflow用户
提问于 2018-02-12 17:15:26
回答 1查看 36关注 0票数 0

在我的应用程序中,我扩展了Dialog class以获取每个字段的用户输入,现在我想验证它们。

代码语言:javascript
复制
public abstract class EditDialogHelper extends Dialog implements android.view.View.OnClickListener {

    private Context context;
    private String title;
    private String field;
    private String positive;
    private String negative;
    private EditText etField;
    private TextView tvCount;
    private int characterCount;

    public EditDialogHelper(Context context, String title, String field, String positive, String negative, int characterCount) {
        super(context);
        this.context = context;
        this.title = title;
        this.field = field;
        this.positive = positive;
        this.negative = negative;
        this.characterCount = characterCount;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_edit_view);

        TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
        etField = (EditText) findViewById(R.id.etField);
        tvCount = (TextView) findViewById(R.id.tvInputCount);
        Button btnConfirmationOk = (Button) findViewById(R.id.btnPositive);
        Button btnConfirmationCancel = (Button) findViewById(R.id.btnNegative);

        final TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvCount.setText(String.valueOf(characterCount - s.length()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        etField.addTextChangedListener(textWatcher);
        tvTitle.setText(title);
        etField.setText(field);
        etField.setFilters(new InputFilter[]{new InputFilter.LengthFilter(characterCount)});
        btnConfirmationOk.setOnClickListener(this);
        btnConfirmationCancel.setOnClickListener(this);
    }

    public String getValue() {
        return  etField.getText().toString().trim();
    }



    private boolean validateInputs(String value) {
        boolean valid = false;
        if (value != null && !(value.equals(""))) {
            valid = true;
        } else {
            etField.setError("This can't be left empty");
        }
        return valid;
    }


}

对话框打开后,我希望在单击btnConfirmationOk时对其进行验证,如果该字段为空,则应防止它在显示错误时关闭对话框。

我应该在哪里使用这个validateInputs方法,以及应该以什么方式修改它。

EN

回答 1

Stack Overflow用户

发布于 2018-02-12 17:31:04

我想答案很简单

代码语言:javascript
复制
@Override
void onClick(View view) {
      if (view.getId == R.id.btnPositive) {
       boolean valid = validate("my string");
         if(valid) {
           // do stuff
           dissmis();
         }
        } else {
           dissmis();
        }

}

但在我的观点中,你应该为你的肯定和否定按钮设置不同的监听器,而不是用EditDialogHelper类来跟踪所有的事情。

这可以像这样做。

代码语言:javascript
复制
button.setOnClickListener(new OnClickListener {

   @Override
   void onClick(View v) {
   }    

});

附注:我从我的头脑中写下了所有的东西,所以这可能包含编译错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48742899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档