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