简体   繁体   English

在Android中按下EditText输入执行操作同时保留键盘上的输入图标?

[英]Perform action on EditText enter pressed in android while retaining the enter icon on keyboard?

I want to perform an action when enter is pressed in android editText.我想在 android editText 中按下 Enter 时执行一个操作。 I have tried various ways of using EditorInfo.IME_ACTION_DONE , which works, but the problem is, it shows a tick icon, which I don't want.我尝试了各种使用EditorInfo.IME_ACTION_DONE ,这很有效,但问题是,它显示了一个我不想要的勾号图标。 I want it to look like the default enter button on the keyboard.我希望它看起来像键盘上的默认输入按钮。

How do I handle enter pressed event while retaining the "Enter" shape?如何在保留“输入”形状的同时处理输入按下事件? Is there something like EditorInfo.IME_ACTION_ENTER to get my job done?有没有像EditorInfo.IME_ACTION_ENTER这样的东西来完成我的工作?

My approach (which shows tick icon):我的方法(显示勾号图标):

editorEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if( (actionId == EditorInfo.IME_ACTION_DONE){
            Toast.makeText(EditorActivity.this, "working", Toast.LENGTH_SHORT).show();

            return true;
        }

        return false;
    }
});

I want it to look like "Enter" shape because I am making a text editor.我希望它看起来像“输入”形状,因为我正在制作一个文本编辑器。 I am trying to shift to another EditText on the "Enter" button is pressed and don't want it to look like a "Tick" button.我试图在按下“Enter”按钮时切换到另一个EditText并且不希望它看起来像“Tick”按钮。 That's why I don't want the tick symbol.这就是为什么我不想要刻度符号。

I think just changing the inputType of your EditText in your layout should do the trick to have an "Enter" like button in your soft keyboard in Android.我认为只需在布局中更改EditTextinputType就可以在 Android 的软键盘中使用类似“Enter”的按钮。

<EditText
    android:id="@+id/edit"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="textMultiLine" />

Please note that I have used textMultiLine as the inputType of the EditText .请注意,我使用textMultiLine作为EditTextinputType

You can take some action on pressing the "Enter" button in your EditText like the following.您可以在EditText按下“Enter”按钮来执行一些操作,如下所示。

final EditText edittext = (EditText) findViewById(R.id.edit);

edittext.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (before == 0 && count == 1 && s.charAt(start) == '\n') {
            String text = edittext.getText().replace(start, start + 1, "").toString(); // Removes the enter
            Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});

Hope that completes the answer.希望回答完毕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM