开发者

Dispatching a KeyEvent after the current event is finished?

开发者 https://www.devze.com 2023-04-13 01:39 出处:网络
I have an OnKeyListener(), living in a class that extends an EditText, that when the enter key is pressed checks for a certain character at the end of the line and if it exists 开发者_开发技巧dispatch

I have an OnKeyListener(), living in a class that extends an EditText, that when the enter key is pressed checks for a certain character at the end of the line and if it exists 开发者_开发技巧dispatches another KeyEvent to send a tab press back to the EditText. But what is happening is it adds the tab and then creates a new line. I need the tab after the new line is created. Is there anything that gets triggered after the OnKeyListener()/OnKey() methods that I could put my "tab event"? Or another way? Thanks.


You could append the linefeed and tab in your onKey method and then return true to indicate that the event has been consumed to prevent the original linefeed from being processed afterward.

public boolean onKey (final View view, final int i, final KeyEvent keyEvent) {
    Editable editableText = ((EditText)view).getText();

    if (shouldAppendTab(editableText)) {
        editableText.append('\n');
        editableText.append('\t');
        return true;
    }
    return false;
}

Replace shouldAppendTab(editableText) with your code for determining whether you want the tab to be appended.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号