How to have an EditText stuck to the soft keyboard in Android

江枫思渺然 提交于 2020-01-14 13:59:22

问题


I'm trying to make something like the image below, where the keyboard automatically opens when the activity starts and the EditText and the send button stick 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" />


来源:https://stackoverflow.com/questions/32536827/how-to-have-an-edittext-stuck-to-the-soft-keyboard-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!