软键盘切断了我的EditText / TextInputEditText控件的底部

时间:2018-01-23 13:30:12

标签: java android android-layout android-softkeyboard

我遇到了下图所示的挑战;

Soft Keyboard overlaps EditText Control

我没有遇到过这种挑战的唯一一次是当EditText控件没有用于实现控件边框的drawable时。

我尝试了以下建议,似乎没有解决它;

建议1

@Override
//public void setBackground(Drawable background) {  
public void setBackgroundResource(int resid) {
    int pl = getPaddingLeft();
    int pt = getPaddingTop();
    int pr = getPaddingRight();
    int pb = getPaddingBottom();

    super.setBackgroundResource(resid);

    this.setPadding(pl, pt, pr, pb);
}

建议2

android:windowSoftInputMode="adjustResize" 

建议3

android:windowSoftInputMode="adjustPan" 

如何让EditText控件进入软键盘?

更新1

我的布局的一部分;

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint=" "
    >
    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="0px"
        android:layout_height="0px"/>
    <android.support.design.widget.TextInputEditText
        android:id="@+id/txtUserName"
        android:layout_width="match_parent"
        android:layout_marginBottom="@dimen/textControlMarginBottom"
        android:hint="@string/userNameHint"
        android:inputType="text"
        android:nextFocusUp="@id/txtUserName"
        android:nextFocusLeft="@id/txtUserName"
        android:nextFocusDown="@+id/txtPassword"
        android:nextFocusRight="@+id/txtPassword"
        style="@style/EditTextViewSingleRowStyle" />
</android.support.design.widget.TextInputLayout>
<style name="EditTextViewSingleRowStyle">
    <item name="android:layout_height">@dimen/textViewHeight</item>
    <item name="android:paddingTop">@dimen/textViewTopPadding</item>
    <item name="android:paddingBottom">@dimen/textViewBottomPadding</item>
    <item name="android:background">@drawable/selector_text_control_edit</item>
    <item name="android:cursorVisible">true</item>
    <item name="android:fontFamily">sans-serif</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:maxLines">1</item>
    <item name="android:singleLine">true</item>
    <item name="android:textColor">@color/controlTextColor</item>
    <item name="android:textColorHint">@color/controlTextViewHint</item>
    <item name="android:textCursorDrawable">@drawable/cursor</item>
    <item name="android:textIsSelectable">true</item>
    <item name="android:textSize">@dimen/controlTextSize</item>
</style>

解决方案

我为EditText / TextInputEditText控件设置了一个固定的高度。我将其从android:layout_height='48dp'更改为android:layout_height='match_content'

3 个答案:

答案 0 :(得分:0)

添加到您的活动ScrollView

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >  


</ScrollView>

然后在你的activity内确保你正在做这样的事情(这段代码只是为了演示在哪里使用 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);)

例如:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    final EditText edittext= (EditText)findViewById(R.id.edittext1);
    edittext.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            edittext.requestLayout();
            MyActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);

            return false; 
        } 
    }); 


     } 

答案 1 :(得分:0)

您应该将父布局用作RelativeLayout 然后在android:layout_alignParentBottom="true"中添加TextInputEditText,就像这样..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"> 

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:hint="username ">
        <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->

        <android.support.design.widget.TextInputEditText
            android:id="@+id/txtUserName"
            style="@style/EditTextViewSingleRowStyle"
            android:layout_width="match_parent"
            android:layout_height="46dp" 
            android:background="@drawable/edittextBorder"
            android:layout_marginBottom="10dp"
            android:inputType="text"
            android:nextFocusDown="@+id/txtPassword"
            android:nextFocusLeft="@id/txtUserName"
            android:nextFocusRight="@+id/txtPassword"
            android:nextFocusUp="@id/txtUserName" />
    </android.support.design.widget.TextInputLayout> 

</RelativeLayout>

您可以通过在drawable文件夹中添加此edittextBorder.xml来自定义TextInputEditText

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <size android:height="5dp" />
    <padding
        android:bottom="15dp"
        android:top="15dp" />

    <!-- Background Color -->
    <solid android:color="#f2f1f1" />

    <!-- Border Color -->
    <stroke
        android:width="2dp"
        android:color="#b90917" />

    <!-- Round Corners -->
    <corners android:radius="5dp" />

</shape>

并像这样输出..

enter image description here

答案 2 :(得分:0)

我为EditText / TextInputEditText控件设置了一个固定的高度。我将其从android:layout_height='48dp'更改为android:layout_height='match_content'