ANDROID:如何确保用户将特定字符串输入编辑文本?

时间:2012-04-27 13:26:20

标签: java android android-emulator

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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
        }
    };
相关问题