有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java软键盘切断了EditText/TextInputText控件的底部

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

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

安卓:windowSoftInputMode="adjustResize" 

建议3

安卓:windowSoftInputMode="adjustPan" 

如何让EditText控件位于软键盘上

更新1

我布局的一部分

<安卓.support.design.widget.TextInputLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:hint=" "
    >
    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        安卓:focusable="true"
        安卓:focusableInTouchMode="true"
        安卓:layout_width="0px"
        安卓:layout_height="0px"/>
    <安卓.support.design.widget.TextInputEditText
        安卓:id="@+id/txtUserName"
        安卓:layout_width="match_parent"
        安卓:layout_marginBottom="@dimen/textControlMarginBottom"
        安卓:hint="@string/userNameHint"
        安卓:inputType="text"
        安卓:nextFocusUp="@id/txtUserName"
        安卓:nextFocusLeft="@id/txtUserName"
        安卓:nextFocusDown="@+id/txtPassword"
        安卓:nextFocusRight="@+id/txtPassword"
        style="@style/EditTextViewSingleRowStyle" />
</安卓.support.design.widget.TextInputLayout>

<style name="EditTextViewSingleRowStyle">
    <item name="安卓:layout_height">@dimen/textViewHeight</item>
    <item name="安卓:paddingTop">@dimen/textViewTopPadding</item>
    <item name="安卓:paddingBottom">@dimen/textViewBottomPadding</item>
    <item name="安卓:background">@drawable/selector_text_control_edit</item>
    <item name="安卓:cursorVisible">true</item>
    <item name="安卓:fontFamily">sans-serif</item>
    <item name="安卓:gravity">center_vertical</item>
    <item name="安卓:maxLines">1</item>
    <item name="安卓:singleLine">true</item>
    <item name="安卓:textColor">@color/controlTextColor</item>
    <item name="安卓:textColorHint">@color/controlTextViewHint</item>
    <item name="安卓:textCursorDrawable">@drawable/cursor</item>
    <item name="安卓:textIsSelectable">true</item>
    <item name="安卓:textSize">@dimen/controlTextSize</item>
</style>

解决方案

我已经为EditText/TextInputText控件设置了一个固定的高度。我把它从安卓:layout_height='48dp'改为安卓:layout_height='match_content'


共 (3) 个答案

  1. # 1 楼答案

    我已经为EditText/TextInputText控件设置了一个固定的高度。我把它从android:layout_height='48dp'改为android:layout_height='match_content'

  2. # 2 楼答案

    您应该使用父布局作为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,可以自定义TextInputEditText。可绘图文件夹中的xml

    <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

  3. # 3 楼答案

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