如何触发 Android 键盘上的 shift 键?
How to trigger shift-button on Android keyboard?
如何触发 Android 键盘上的 "shift-button",以便下一个字符变为大写?
我将 InputType
设置为 InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_MULTI_LINE
,但如果光标在行中的任何位置,这将不起作用。
我试过了。两者都不起作用。第二个抛出 RuntimeException
("This method cannot be called from the main application thread")。可能是因为此按钮没有键码。
有没有办法触发按钮?
编辑:
似乎像 shift 和 alt 这样的按钮会创建影响按下按钮的元状态。来自 https://developer.android.com/reference/android/view/KeyEvent.html:
Meta states describe the pressed state of key modifiers such as META_SHIFT_ON or META_ALT_ON.
可以使用 dispatchKeyEvent
模拟这些元状态,如下所示:
dispatchKeyEvent(new KeyEvent(0,0,KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_L,0,KeyEvent.META_SHIFT_ON));
如果 EditText
处于焦点状态,这会将 "L" 写入其中。但似乎你不能只发送 shift KeyEvent
并期望下一个字符是大写的。也许您可以想出一些解决方法。
您正在使用 EditText
,对吧?由于某种原因,我无法获得
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT));
当 EditText 获得焦点时工作,而 KeyEvents
字母和数字工作。
解决方法是将 TextWatcher
添加到 EditText
:
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged: "+s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(TAG, "beforeTextChanged: "+s.toString());
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG, "onTextChanged: "+s.toString());
}
});
并检查CharSequence s
中想要的字符是否在afterTextChanged
中大写。如果不是,则将其更改为大写版本。这有点老套,并没有完全回答你的问题我知道但不幸的是我无法发表评论。
如何触发 Android 键盘上的 "shift-button",以便下一个字符变为大写?
我将 InputType
设置为 InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_MULTI_LINE
,但如果光标在行中的任何位置,这将不起作用。
我试过了RuntimeException
("This method cannot be called from the main application thread")。可能是因为此按钮没有键码。
有没有办法触发按钮?
编辑:
似乎像 shift 和 alt 这样的按钮会创建影响按下按钮的元状态。来自 https://developer.android.com/reference/android/view/KeyEvent.html:
Meta states describe the pressed state of key modifiers such as META_SHIFT_ON or META_ALT_ON.
可以使用 dispatchKeyEvent
模拟这些元状态,如下所示:
dispatchKeyEvent(new KeyEvent(0,0,KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_L,0,KeyEvent.META_SHIFT_ON));
如果 EditText
处于焦点状态,这会将 "L" 写入其中。但似乎你不能只发送 shift KeyEvent
并期望下一个字符是大写的。也许您可以想出一些解决方法。
您正在使用 EditText
,对吧?由于某种原因,我无法获得
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT));
当 EditText 获得焦点时工作,而 KeyEvents
字母和数字工作。
解决方法是将 TextWatcher
添加到 EditText
:
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged: "+s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(TAG, "beforeTextChanged: "+s.toString());
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG, "onTextChanged: "+s.toString());
}
});
并检查CharSequence s
中想要的字符是否在afterTextChanged
中大写。如果不是,则将其更改为大写版本。这有点老套,并没有完全回答你的问题我知道但不幸的是我无法发表评论。