java 如何将键事件发送到编辑文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4779000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send key event to an edit text
提问by virsir
For example, send a backspace key to the edit text control to remove a character or send a char code like 112 to append a character in the edittext control programmatically.
例如,向编辑文本控件发送一个退格键以删除一个字符或发送一个像 112 这样的字符代码以通过编程方式在编辑文本控件中附加一个字符。
Actually, I need a method like
实际上,我需要一种方法,例如
void onKeyReceived(int keyCode)
{
// here I would like to append the keyCode to EditText, I know how to add a visible character, but what about some special keys, like arrow key, backspace key.
}
回答by Torben
To send a simulated backspace key press to an EditText you have to send both key press and release events. Like this:
要将模拟退格键按下发送到 EditText,您必须同时发送按键按下和释放事件。像这样:
mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DEL, 0));
mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_DEL, 0));
This can be used to send any other key code, not just delete.
这可用于发送任何其他密钥代码,而不仅仅是删除。
回答by dave.c
Your question is not all that clear, but I think you want to modify/append text to a TextView
when certain buttons are pressed. If so, you want a combination of some of the existing answers.
你的问题不是很清楚,但我认为你想TextView
在按下某些按钮时修改/附加文本到 a 。如果是这样,您需要一些现有答案的组合。
@Override
public void onCreate(Bundle savedInstanceState) {
...
(TextView) textView = (TextView) findViewById(R.id.myTextView);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
// user pressed the "BACK" key. Append "_back" to the text
textView.append("_back");
return true;
case KeyEvent.KEYCODE_DEL:
// user pressed the "BACKSPACE" key. Append "_del" to the text
textView.append("_del");
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
Whether to return true
for each case you have handled (as above) or to always return super.onKeyDown(keyCode, event);
after your switch
statement will depend on your exact requirements. Check the documentation for the behaviour of onKeyDown
是true
针对您处理过的每个案例返回(如上所述)还是始终super.onKeyDown(keyCode, event);
在您的switch
陈述后返回将取决于您的确切要求。检查文档以了解onKeyDown
If, instead of appending text in each case you want to delete a character, or move the cursor, you could do that in each case
statement. Have a look at the TextView
documentationfor the different methods you can call. Also look at the KeyEvent
documentationfor a list of the keys you can check for.
如果不是在每种情况下都附加文本,而是要删除字符或移动光标,则可以在每个case
语句中执行此操作。查看您可以调用的不同方法的TextView
文档。另请查看KeyEvent
文档以获取您可以检查的密钥列表。
回答by Labeeb Panampullan
I think you need use addTextChangedListener
to EditText
.
Refer the answer of EditText input with pattern androidand Live editing of users input
我认为你需要使用addTextChangedListener
到EditText
。
参考EditText input with pattern android和用户输入的实时编辑的答案
回答by 100rabh
virsir , I suppose you are looking for dispatching hard keys programmatically.
virsir ,我想您正在寻找以编程方式发送硬键。
For that you may try dispatch (KeyEvent.Callback receiver, KeyEvent.DispatcherState state, Object target)with an example at Back and other hard keys: three stories
为此,您可以尝试dispatch(KeyEvent.Callback 接收器、KeyEvent.DispatcherState 状态、Object 目标),并以Back 和其他硬键为例:三个故事
Hope that helps.
希望有帮助。
回答by Harry Joy
Take a look at this article: creating-input-method.html. Basically, you can either manually send KeyEvents
or you can manually edit and commit text around the cursor
in the application's Input View
.These are all done via your IME's InputConnection
.
看看这篇文章:created-input-method.html。基本上,您可以手动发送KeyEvents
,也可以edit and commit text around the cursor
在应用程序中手动发送Input View
。这些都是通过您的 IME 完成的InputConnection
。
Hope this helps,
希望这可以帮助,
回答by Aman Alam
Check for key events in your activity. for example, this code listens for back
keypress:
检查活动中的关键事件。例如,此代码侦听back
按键:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
回答by N-JOY
try implementing TextWatcher interface.
尝试实现 TextWatcher 接口。
it has 3 methods which you need to override.
它有 3 个你需要重写的方法。
public void afterTextChanged(Editable s) {
Log.v("afterTextChanged","here");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.v("beforeTextChanged","here");
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
I think this will work.
我认为这会奏效。
回答by Nathan Schwermann
just use the setText method to do this. If you are wanting to simulate a backspace you could do something like this.
只需使用 setText 方法即可。如果你想模拟退格,你可以做这样的事情。
String curText = mEditText.getText();
if(!curText.equals("")){
mEditText.setText(curText.subString(0, curText.length - 1));
}
回答by Ephraim
if you want a click listener, the best way to do it is this:
如果你想要一个点击监听器,最好的方法是这样的:
View textfield = findViewById(R.id.textfield);
textfield .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*your code for the click event here*/ }});
if you want a backspace button, do this:
如果您想要退格按钮,请执行以下操作:
public void backSpace() {
EditText textfield = (EditText)findViewById(R.id.textfield);
try {
textfield.getText().delete(textfield.getSelectionEnd() - 1, textfield.getSelectionStart());
} catch (Exception e) {
try {
textfield.getText().delete(textfield.length() - 1, textfield.length());
} catch (Exception myException) {
//textfield.getText().delete(textfield.length(), textfield.length() - 1);
}
}
}
if you want to append a character in the EditText, do this:
如果要在 EditText 中附加一个字符,请执行以下操作:
EditText textfield = (EditText)findViewById(R.id.textfield);
textfield.setText(textfield.getText().concat("112"));
回答by Buda Gavril
to simulate backspace key, just ad code
模拟退格键,只需广告代码
editText.setText(editText.getText().substring(0,editText.getText().length()-1))
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
to simulate adding a character, put the code
模拟添加字符,放代码
editText.setText(editText.getText() + (char) charCode)
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);