android - 在保持焦点的同时禁用 EditText 输入
问题描述
我有一个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)
}
}
我试过上面的方法,它不起作用。如何实现我所需要的?
解决方案
也许您应该尝试以下逻辑
maxLength=inputChars.length
if (!inputChars.matches(MY_REGEXP)) {
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
}
当与正则表达式不匹配时,上面的程序将设置 EditText 的最大长度。
推荐阅读
- authentication - 如何对 Google Cloud Endpoint 进行经过身份验证的调用?
- elixir - 如何在 Absinthe GraphQL 请求中接受 JSON
- python - 在 Python 中为数据找到合适的多项式拟合
- django - 使用电子邮件而不是用户名获取身份验证令牌
- reactjs - Firebase - 服务器端渲染的服务器 Firestore 读取的最佳实践
- c# - 如何使用 WCF 接收从 Android 发送的图像?
- haskell - 将数字列表转换为自然数
- python - 在使用 Keras LSTM 拟合和预测数据期间,如何使用不同大小的训练数据和测试数据以及不同的 batch_size?
- sql - 比最近更早的历史记录
- jquery - jQuery 全选复选框;取消选中子复选框时取消选择父