How to have an EditText stuck to the soft Keyboard

2019-07-08 19:51发布

问题:

I'm trying to make something like the Image below, the keyboard automatically opens when the start the activity and the Edittext and the send button stuck to the keyboard.

回答1:

Use the below code to pop up the soft keyboard automatically when an activity launches

InputMethodManager imm = (InputMethodManager)getSystemService(
    Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(youredittext, 0);

Make sure that you have not define android:windowSoftInputMode="stateHidden" in your manifest.xml.

To make an Edittext attach with the footer,use the following code:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#f3f3f3"
        android:paddingBottom="10.0dip"
        android:paddingTop="10.0dip"
        android:id="@+id/bottom_bar" >

        <EditText
            android:id="@+id/et_send_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10.0dip"
            android:layout_toLeftOf="@+id/ib_send"
            android:hint="Enter Message"
            android:singleLine="true" />

        <ImageView
            android:id="@+id/ib_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/et_send_bar"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/et_send_bar"
            android:layout_marginBottom="1px"
            android:layout_marginRight="10.0dip"
            android:layout_marginTop="1px"
            android:background="@drawable/chatsend_bg"
            android:paddingBottom="5.0dip"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="5.0dip"
            android:src="@drawable/ic_send_dark_normal" />
    </RelativeLayout>


回答2:

To show Keyboard at start of Activity you need to use like this:

 <activity android:name="com.your.package.ActivityName"
          android:windowSoftInputMode="stateVisible"  />

From Android Docs:

stateVisible

The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window).

To hide Keyboard at start of Activity you need to use like this:

In your AndroidManifest.xml:

<activity android:name="com.your.package.ActivityName"
          android:windowSoftInputMode="stateHidden"  />

From Android Docs:

stateHidden

The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.

This setting will hide soft keyboard when user enters new Activity (even if EditText control gains the focus). Soft keyboard will be shown only when user clicks the edit box control.



回答3:

I have also found another solution for moving up layout components when Soft keyboard appers.

It can be achieved using adjustResize attribute in AndroidManifest.xml

Main purpose of adjustResize attribute is the activity's main window is always resized to make room for the soft keyboard on screen.

To show Keyboard and moved up EditText at start of Activity you need to use like this:

<activity android:name="com.your.package.ActivityName"
          android:windowSoftInputMode="adjustResize" />


回答4:

i think it will works for you: Add this statement in manifest file to your Activity: android:windowSoftInputMode="stateHidden"

  <activity
        android:name="ConversationActivity"
        android:label="@string/title_activity_conversations"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden" />