简体   繁体   English

如何在 Android 中将 EditText 粘在软键盘上

[英]How to have an EditText stuck to the soft keyboard in Android

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.我正在尝试制作类似下图的东西,当活动开始时键盘会自动打开,并且EditText和发送按钮粘在键盘上。

在此处输入图片说明

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.确保您没有在 manifest.xml 中定义android:windowSoftInputMode="stateHidden"

To make an Edittext attach with the footer,use the following code:要使用页脚附加 Edittext,请使用以下代码:

<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>

To show Keyboard at start of Activity you need to use like this:要在Activity开始时显示键盘,您需要像这样使用:

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

From Android Docs:来自 Android 文档:

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:要在Activity开始时隐藏键盘,您需要像这样使用:

In your AndroidManifest.xml:在您的 AndroidManifest.xml 中:

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

From Android Docs:来自 Android 文档:

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).此设置将在用户进入新 Activity 时隐藏软键盘(即使 EditText 控件获得焦点)。 Soft keyboard will be shown only when user clicks the edit box control.仅当用户单击编辑框控件时才会显示软键盘。

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可以使用AndroidManifest.xml adjustResize属性来实现

Main purpose of adjustResize attribute is the activity's main window is always resized to make room for the soft keyboard on screen. adjustResize属性的主要目的是 Activity 的主窗口总是调整大小,以便为屏幕上的软键盘腾出空间。

To show Keyboard and moved up EditText at start of Activity you need to use like this:要在Activity开始时显示键盘并向上移动EditText ,您需要像这样使用:

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

i think it will works for you: Add this statement in manifest file to your Activity: android:windowSoftInputMode="stateHidden"我认为它对您有用:将清单文件中的此语句添加到您的活动中: android:windowSoftInputMode="stateHidden"

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

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

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