【问题标题】:Disable EditText inputs while keeping it focused在保持焦点的同时禁用 EditText 输入
【发布时间】:2018-11-22 19:16:06
【问题描述】:

我有一个EditText 字段,变量名valueInputField

我听文本输入的变化,我想要实现的是在输入过程中,当输入格式不匹配正则表达式时,我想停止显示更多输入,但保持字段集中&用户能够在键盘上键入仍然不会对现场产生任何实际影响。

@OnTextChanged(R.id.value_input)
protected void onTextChanged(CharSequence charSequence) {
   String inputChars = charSequence.toString().trim();
   // if doesn't match regular expression, stop showing more inputs
   if (!inputChars.matches(MY_REGEXP)) {
      // this doesn't achieve what I need.
      valueInputField.setInputType(0)
   }
}

我试过上面的方法,它不起作用。如何实现我所需要的?

【问题讨论】:

  • 在 XML 中设置 android:maxLength="3"'会做你想做的事。
  • valueInputField.requestFocus();但实际上你不必做所有这些事情,Vir 是对的
  • 其实对不起,我问错了,我现在更新我的问题。
  • 你需要实现public void beforeTextChanged(CharSequence s, int start, int count, int after)
  • 能否请您提供您的完整代码以及您想要什么样的输出

标签: android android-edittext


【解决方案1】:

也许你应该试试下面的逻辑

maxLength=inputChars.length
if (!inputChars.matches(MY_REGEXP)) {
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
}

当EditText不匹配regexp时,上面的程序会设置一个最大长度。

【讨论】:

  • 用户在使用退格后仍然可以使用退格或更改文本
  • 显然,这就是 SO 正在寻找的答案。请参阅我的答案的 cmets 部分。
【解决方案2】:

你必须在你的 textView 中添加一个 InputFilter:

这是一个例子:

editText.filters = arrayOf(object: InputFilter {
   override fun filter(source: CharSequence?, start: Int, end: Int, dest: Spanned?, dstart: Int, dend: Int): CharSequence {
      if (Regex("[0-9]*").matches(source!!)) {
         return source
      } else {
         return source.subSequence(start, end-1)
      }
   }
})

每次调用 editText 中的输入时都会调用此过滤器方法。您可以在匹配正则表达式时返回源,否则,您可以返回源的子序列,而不插入最后一个字符。

代码示例在 Kotlin 中,翻译应该不会给您带来任何问题 :)

【讨论】:

  • 请注意,source 将始终为您提供迄今为止输入的所有字符
  • @user1506104 这就是为什么我将源与正则表达式匹配,整个输入需要匹配模式
【解决方案3】:

你已经在做setInputType(InputType.TYPE_NULL)。但它应该与 withsetEnabled(false) 一起使用才能正常工作。

这也会改变背景颜色。强烈推荐它,因为它为用户提供了它不会接受输入的线索。但是,如果您愿意,可以将其更改为 setBackgroundColor(Color.TRANSPARENT)

如果要隐藏光标,添加setCursorVisible(false)

您也可以使用afterTextChanged 代替onTextChanged,这样您就可以操作文本。

【讨论】:

    【解决方案4】:

    如果您想停止编辑文本中的建议,您应该尝试以下操作:

    editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    

    并在任何条件后再次启用:

    editText.setInputType(InputType. TYPE_CLASS_TEXT);
    

    这是一个带有大小阈值的输入示例,这里也适用于您的正则表达式:

    editText.addTextChangedListener(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) {
                if(s.length()< threshold){
                    editText.setInputType(InputType. TYPE_CLASS_TEXT);
                }
                if(s.length()>= threshold){
                    editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                }
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
            }
        });
    

    【讨论】:

    • @Leem 这对你有帮助吗?
    【解决方案5】:

    禁用编辑文本的输入类型在聚焦或编辑文本时无法正常工作。一旦将 inout 类型设置为 null,视图将​​开始接收按键回调方法。因此,有类变量来验证字段并取消按下的键。 这里是示例代码。

    public class SplashActivity extends AppCompatActivity{
    
        EditText valueInputField;
        private  boolean allowValueField2Edit = true;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ....
    
            valueInputField.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2{
                }
    
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                    String inputChars = charSequence.toString().trim();
                    // if doesn't match regular expression, stop showing more inputs
                    if (!inputChars.matches(MY_REGEXP)) {
    
                        // this doesn't achieve what I need.
                        valueInputField.setInputType(InputType.null);
                        allowValueField2Edit = false;
                    }
    
                }
    
                @Override
                public void afterTextChanged(Editable editable) {
                }
            });
    
          //Once you set the input type to none fore edit control, on Key lister will receive callbacks
            valueInputField.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                 //Here, you can add your custom logic to have key actions.
                //retrun false will have key effect, return true 
                    return !allowValueField2Edit;
                }
            });
    

    希望这会有所帮助...

    【讨论】:

    • 这不适用于软键盘stackoverflow.com/questions/3697063/…
    • 一旦您将编辑文本字段输入类型设置为空或禁用,按键监听器将起作用。我同意它在编辑模式下不起作用。
    【解决方案6】:

    伪代码:

    1. 等待输入文本与您的模式匹配
    2. 找到匹配项时,设置过滤器
    3. 过滤器需要在每次按键时返回相同的先前字符串

    源代码:

    editText.addTextChangedListener(new TextWatcher() {
        ...
        @Override
        public void afterTextChanged(Editable editable) {
            String text = editable.toString();
    
            // STEP 1
            if(text.matches(myRegex) && !flag) {
    
                // make sure that this code block is executed only once
                flag = true;
    
                // STEP 2
                // when match is found, always return the same string
                // therefore disabling further editing of the editText field
                editText.setFilters(new InputFilter[]{
                    new InputFilter() {
                        @Override
                        public CharSequence filter(CharSequence charSequence, int i, int i1, 
                                Spanned spanned, int i2, int i3) {
                            // STEP 3
                            return spanned.subSequence(i2, i3);
                        }
                    }
                });
            }
        }
    });
    

    在设置输入过滤器时,请注意正确的设置方法。 Please check this SO answer by Steven Byle

    【讨论】:

    • 我运行了这段代码,但是键盘删除按钮失去了它的作用。用户无法删除输入。
    • 我以为你说的是​​“禁用 EditText 输入”。如果不是,那么在 match 上设置 maxlength 应该可以。
    猜你喜欢
    • 2021-04-26
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多