如何在EditText和软键盘之间添加边距?

39

我想在EditText和软键盘之间添加10dp的边距。

enter image description here 这是我的XML代码:

<RelativeLayout
    android:id="@+id/BottomLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@drawable/bottom_glass" >

    <EditText
        android:id="@+id/message"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/grey"
        android:ems="10"
        android:layout_marginBottom="10dp"
        android:hint="Enter Message"
        android:textColor="#ffffff" >


    </EditText>

    <ImageButton
        android:id="@+id/sendMessageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/message"
        android:background="@android:color/transparent"
        android:src="@drawable/sendselector" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:layout_toLeftOf="@+id/message"
        android:background="@android:color/transparent"
        android:src="@drawable/sendmediaselector" />

</RelativeLayout>

这是我的 onCreate() :

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    messageText.setOnTouchListener(new OnTouchListener(){

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                            ((Swipe)getActivity()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
                    return false;
                }});

我正在使用viewpager和全屏主题。下面的EditText位于我的第3页。

我已经使用了android:windowSoftInputMode="adjustResize|adjustPan"android:windowSoftInputMode="adjustResize",但没有发生任何事情。


你尝试过改变EditText的填充吗? - W.K.S
不,我想要它在布局的中心。 - Punit
我是 Android 的新手,不知道什么更好。 - Punit
尝试使用 android:layout_gravity="center" - W.K.S
1
填充会增加编辑文本的高度,但我想要在编辑文本和键盘之间留出空间。同时,我正在使用相对布局,如果键盘不遮挡它将更好。它会遮挡一半的相对布局。 - Punit
显示剩余4条评论
6个回答

23

我发现paddingBottom属性可以影响editText和键盘之间的距离。你可以这样做:

<RelativeLayout   
  android:layout_width="match_parent"   
  android:layout_height="110dp"> 
  <ImageView
     android:layout_width="match_parent"
     android:layout_height="100dp"
     android:background="@drawable/your_edit_text_background"/>   
  <EditText
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="10dp"/> 
</RelativeLayout>

这将使您的键盘将您的EditText边缘留出10dp


1
android:paddingBottom="5dp",不是padding_bottom。 - Adnan Abdollah Zaki
4
将提示和光标移动到EditText内部。因此,您输入到EditText中的一些文本可能会被遮挡。 - filthy_wizard
1
太棒了!谢谢!另外,我需要在EditText中添加android:gravity="bottom"。 - Soo Chun Jung
@SooChunJung 那个重力底部解决了我的问题,谢谢! - DIRTY DAVE
我尝试过这个方法,发现将paddingBottom设置到RelativeLayout或ScrollView(无论你使用哪一个)会更好,因为将paddingBottom添加到EditText本身可能会导致设计问题,典型的输入字段下方的行(在大多数设计中)被向下推。 - The Fluffy T Rex
显示剩余2条评论

3

这似乎可以起作用,但会对布局产生一些不必要的影响。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

如果有人知道如何将此与ADJUST_PAN配合使用,请告诉我们,谢谢!

这个能在全屏模式下工作吗?因为我的应用是在全屏模式下的。如果不是全屏,这个可以正常工作... - Punit
不太确定,也许其他人可以分享他们的经验。 - Veeru
ADJUST_RESIZE将无法与全屏一起使用。PAN不确定,但可能是相同的。 - mithunc

1
尝试将您的RelativeLayout添加到ScrollView中,然后添加以下代码:
editTextField.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub
        if (hasFocus)
             scrollView.scrollBy(0, 150);

    });

谢谢!如果您在NestedScrollView中嵌套了RecyclerView,请滚动NestedScrollView而不是RecyclerView。此外,在键盘出现后可能需要使用postDelayed添加短暂延迟。不需要使用RelativeLayout - CoolMind
这段代码在我的电脑上没有运行成功。 - Ucdemir
如果您在300毫秒后调用 scrollView.scrollBy(0, 150);,它将起作用。 - Ucdemir

1

Vasily Kabunov的回答对我有用,但他的回答需要做一点改动才能正常工作。下面是代码:

editTextField.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub
        if (hasFocus) {
                Handler(Looper.getMainLooper()).postDelayed({
                    //Take care with this line I think that the scroll moves in px and not with dp.
                    scrollView.scrollBy(0, 80)
                }, 100) //You need to play with the time if your activity comes from onCreate and onResume you need to change this value to 1000.
            }

    });
}

你需要在你的ScrollView或RecyclerView中加入以下代码:

android:paddingBottom="80dp"
android:clipToPadding="false"

重要提示: 如果您已经激活了全屏模式,则标志“adjustResize”和“adjustPan”将无效。


0

我通过添加两行代码解决了同样的问题

EditText中添加以下代码 -

android:imeOptions="actionSend|flagNoEnterAction"

并且在Manifest文件中的Activity标签中添加这一行 -

<activity
            android:name="----"
            android:icon="---"
            android:windowSoftInputMode="stateVisible|adjustResize"
            android:label="@string/app_name"
            android:theme="-----" />

并且移除你用来执行此操作的所有代码, 如 onTouchListner ,然后它应该能正常工作。


-1
在清单文件中的活动中添加android:windowSoftInputMode="adjustResize"应该可以解决这个问题。 如果您的活动正在全屏运行,则此方法将无效。因此,在这种情况下,不要通过代码使其全屏,而是将样式<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">添加或更改到style.xml,并将其用作您的AppTheme。 adjustResize或adjustPan都可以使用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接