如何将按键事件发送到编辑文本

发布于 2024-10-13 06:12:45 字数 308 浏览 7 评论 0原文

例如,向编辑文本控件发送退格键以删除字符,或发送字符代码(如 112)以编程方式在编辑文本控件中附加字符。

实际上,我需要一个像这样的方法

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.
}

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.

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.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

浊酒尽余欢 2024-10-20 06:12:45

要将模拟的退格按键发送到 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));

这可以用于发送任何其他键代码,而不仅仅是删除。

To send a simulated backspace key press to an EditText you have to send both key press and release events. Like this:

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.

So要识趣 2024-10-20 06:12:45

您的问题不太清楚,但我认为您想在按下某些按钮时修改/附加文本到 TextView 。如果是这样,您需要一些现有答案的组合。

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

是否为您处理的每种情况返回 true(如上所述),还是在 switchsuper.onKeyDown(keyCode, event); > 声明将取决于您的具体要求。检查文档以了解 onKeyDown

如果您想要删除字符或移动光标,而不是在每种情况下附加文本,则可以在每个 case 语句中执行此操作。查看 TextView 文档您可以调用的不同方法。另请参阅 KeyEvent 文档您可以检查的键列表。

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.

@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

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 documentation for the different methods you can call. Also look at the KeyEvent documentation for a list of the keys you can check for.

如梦亦如幻 2024-10-20 06:12:45

我认为您需要使用 addTextChangedListenerEditText
请参考 EditText input with pattern android实时编辑用户输入

I think you need use addTextChangedListener to EditText.
Refer the answer of EditText input with pattern android and Live editing of users input

浪荡不羁 2024-10-20 06:12:45

virsir,我想您正在寻找以编程方式调度硬键的方法。

为此,您可以尝试 调度(KeyEvent.Callback 接收器、KeyEvent.DispatcherState 状态、对象目标),示例位于 返回键和其他硬键:三个故事

希望有所帮助。

virsir , I suppose you are looking for dispatching hard keys programmatically.

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

Hope that helps.

最舍不得你 2024-10-20 06:12:45

检查您活动中的关键事件。例如,此代码侦听 back 按键:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
    finish();
    }
    return super.onKeyDown(keyCode, event);
}

Check for key events in your activity. for example, this code listens for back keypress:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
    finish();
    }
    return super.onKeyDown(keyCode, event);
}
镜花水月 2024-10-20 06:12:45

只需使用 setText 方法即可完成此操作。如果你想模拟退格键,你可以这样做。

String curText = mEditText.getText();
if(!curText.equals("")){
    mEditText.setText(curText.subString(0, curText.length - 1));
}

just use the setText method to do this. If you are wanting to simulate a backspace you could do something like this.

String curText = mEditText.getText();
if(!curText.equals("")){
    mEditText.setText(curText.subString(0, curText.length - 1));
}
依 靠 2024-10-20 06:12:45

要模拟退格键,只需添加代码

editText.setText(editText.getText().substring(0,editText.getText().length()-1))
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

来模拟添加字符,放入代码

editText.setText(editText.getText() + (char) charCode)

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

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

软的没边 2024-10-20 06:12:45

看看这篇文章:creating-input-method.html。基本上,您可以手动发送KeyEvents,也可以在应用程序的输入视图中手动编辑和提交光标周围的文本。这些都已完成通过 IME 的 InputConnection

希望这有帮助,

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.

Hope this helps,

黑凤梨 2024-10-20 06:12:45

如果您想要一个点击侦听器,最好的方法是:

View textfield = findViewById(R.id.textfield);  
textfield .setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) {  
/*your code for the click event here*/ }});

如果您想要一个退格按钮,请执行以下操作:

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

如果您想在 EditText 中附加一个字符,请执行以下操作:

EditText textfield = (EditText)findViewById(R.id.textfield);  
textfield.setText(textfield.getText().concat("112"));

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 textfield = (EditText)findViewById(R.id.textfield);  
textfield.setText(textfield.getText().concat("112"));
不一样的天空 2024-10-20 06:12:45

尝试实现 TextWatcher 接口。

它有 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) {
}

我认为这会起作用。

try implementing TextWatcher interface.

it has 3 methods which you need to override.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文