简体   繁体   中英

Android soft keyboard dynamic OK/Enter key

I have a EditText field in my app and I would like the soft keyboard to display an OK button by default. I would also like this OK button to switch to the default Enter button when I press the shift button allowing to create a new line. I didn't finad a way to do that. Right now I just display the OK button using

edit.setImeOptions(EditorInfo.IME_ACTION_DONE);

on a single line. What I need is the same keyboard than the one displayed in Google's Keep app in the main screen 'Add Quick note' field

I have looked at the Compose activity from the Android AOSP sources and have built it:

I copied this class into my test project (package name com.example.test):

https://android.googlesource.com/platform/packages/apps/UnifiedEmail/+/5b8799a/src/com/android/mail/compose/EnterSubject.java

and then created this layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <com.example.test.EnterSubject
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:inputType="textEmailSubject|textAutoCorrect|textCapSentences|textImeMultiLine|textMultiLine"
        android:imeOptions="actionDone|flagNoExtractUi|flagNoFullscreen"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="18dp"
        android:ems="10" >

        <requestFocus />
    </com.example.test.EnterSubject>

</RelativeLayout>

This is basically a refactored version of this file: https://android.googlesource.com/platform/packages/apps/UnifiedEmail/+/5b8799a/res/layout/compose_subject.xml and this file: https://android.googlesource.com/platform/packages/apps/UnifiedEmail/+/5b8799a/res/values/styles.xml

And this has the result: Pressing the shift button turns the next button into a enter button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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