【问题标题】:ANDROID: How to ensure the user inputs a specific string into edit text? [closed]ANDROID:如何确保用户在编辑文本中输入特定的字符串? [关闭]
【发布时间】:2012-05-08 06:04:08
【问题描述】:

我正在制作一个基于问题和答案的应用,并且答案由用户输入并且必须准确。如何设置编辑文本以显示用户提供的答案有效,如果有效,我希望出现一个带有按钮的对话框继续并重试!

【问题讨论】:

  • 您所要求的或多或少是对 java/android 的基本了解。请进一步研究

标签: java android android-emulator


【解决方案1】:

您使用TextWatcher 来检查用户输入的答案是否有效。as

mEditText = (EditText)findViewById(R.id.ET);
mEditText.addTextChangedListener(mTextWatcher);
TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        @Override
        public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                int arg3) {
            temp = s;
        }
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                int arg3) {
        }
        @Override
        public void afterTextChanged(Editable s) {
         //CHECK HERE if answer is valid or not
        }
    };

【讨论】:

  • 谢谢!但我用 check.content equals 代替!它工作正常! :D
【解决方案2】:

最好的办法是使用正则表达式模式,并确保文本框中的文本与该模式匹配。 Here's a good piece of documentation that I used to do this.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-16
    • 2014-05-10
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    相关资源
    最近更新 更多