android - 在 android 上以编程方式启用大写锁定(类似双击 shift)
android - Enable caps lock (double click shift like) programatically on android
这个问题不同于这里已经问过的所有其他问题。
问题与疑问
我希望在打开键盘时像双击(或长按)shift 键一样启用大写锁定。
另一个要求是,如果用户按下 shift 键,则必须禁用大写锁定。
我已经在 Whosebug 中尝试了大多数建议的解决方案,例如 android:inputType="textCapCharacters" 或 setAllCaps(true) 但是大写锁定无法禁用。使用上述解决方案,当用户按下 shift 键时,将插入一个小写字符,然后系统会自动将键盘设置回大写锁定。
这不是我想要的正确方式,我只想在用户第一次打开键盘时启用大写,然后他将自己处理大写状态。
备注
请记住,我是从 "like if I double click (or long press) the shift key" 开始提问的,因为使用 inputType 解决方案时,您会遇到这种情况:
没有像我手动启用大写锁定那样的白色大写破折号:
我找到了解决问题的方法!
我必须继续使用 android:inputType="textCapCharacters" 但是当用户按下 shift 键并键入一个小写字符时,一个textwatcher 删除标志 textCapCharacters.
按照执行此操作的 textwatcher 进行操作:
public class EditTextWatcher implements TextWatcher{
private EditText editText;
public PtlEditTextWatcher(EditText editText) {
this.editText = editText;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void afterTextChanged(Editable s) {
if (editText != null && s.length() > 0 && (editText.getInputType() & InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS) > 0)
if (Character.isLowerCase(s.toString().charAt(s.length() - 1)))
editText.setInputType(editText.getInputType() & ~InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
}
}
它的简单用法是:
addTextChangedListener(new EditTextWatcher(myEditText));
这个问题不同于这里已经问过的所有其他问题。
问题与疑问
我希望在打开键盘时像双击(或长按)shift 键一样启用大写锁定。 另一个要求是,如果用户按下 shift 键,则必须禁用大写锁定。
我已经在 Whosebug 中尝试了大多数建议的解决方案,例如 android:inputType="textCapCharacters" 或 setAllCaps(true) 但是大写锁定无法禁用。使用上述解决方案,当用户按下 shift 键时,将插入一个小写字符,然后系统会自动将键盘设置回大写锁定。
这不是我想要的正确方式,我只想在用户第一次打开键盘时启用大写,然后他将自己处理大写状态。
备注
请记住,我是从 "like if I double click (or long press) the shift key" 开始提问的,因为使用 inputType 解决方案时,您会遇到这种情况:
我找到了解决问题的方法!
我必须继续使用 android:inputType="textCapCharacters" 但是当用户按下 shift 键并键入一个小写字符时,一个textwatcher 删除标志 textCapCharacters.
按照执行此操作的 textwatcher 进行操作:
public class EditTextWatcher implements TextWatcher{
private EditText editText;
public PtlEditTextWatcher(EditText editText) {
this.editText = editText;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void afterTextChanged(Editable s) {
if (editText != null && s.length() > 0 && (editText.getInputType() & InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS) > 0)
if (Character.isLowerCase(s.toString().charAt(s.length() - 1)))
editText.setInputType(editText.getInputType() & ~InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
}
}
它的简单用法是:
addTextChangedListener(new EditTextWatcher(myEditText));