首页 文章

android - 在android上以编程方式启用大写锁定(双击移动)

提问于
浏览
2

This question is different from all the others already asked here.

问题和疑问

我希望大写锁定启用就像我打开键盘时双击(或长按)shift键一样 . 另一个要求是,如果用户按下shift键,则必须禁用大写锁定 .

我已经在stackoverflow中尝试了大多数提议的解决方案,比如android:inputType = "textCapCharacters"或setAllCaps(true)但是会发生的情况是大写锁定无法禁用 . 通过上述解决方案,在按下shift时,用户将以小写字母插入一个单个字符,然后系统自动将键盘设置回大写锁定 .

这不是我想要的正确方法,我只希望在用户第一次打开keybaoard时启用大写,然后他将自己处理大写状态 .

注意

请记住,我使用"like if I double click (or long press) the shift key"启动了问题,因为使用inputType解决方案会出现这种情况:
enter image description here
That has not the white caps dash like if I manually enable caps lock:
enter image description here

1 回答

  • -1

    我找到了问题的解决方案!

    我必须继续使用 android:inputType="textCapCharacters" 但是当用户按下shift键并以小写字母键入单个字符时,textwatcher将删除标志 textCapCharacters .

    按照文字执行者的说法:

    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));
    

相关问题