繁体   English   中英

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

[英]Soft Keyboard cuts off the bottom part of my EditText/TextInputEditText Control

我正经历下图所示的挑战;

软键盘与EditText控件重叠

我唯一没有遇到此挑战的时间是当EditText控件没有可绘制控件来实现控件周围的边框时。

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

建议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'

添加到您的活动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; 
        } 
    }); 


     } 

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

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

您可以通过在可绘制文件夹中添加此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>

然后这样输出

在此处输入图片说明

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

暂无
暂无

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

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