【问题标题】:TextInputLayout with EditText is decreasing when user touch当用户触摸时,带有 EditText 的 TextInputLayout 正在减少
【发布时间】:2017-12-21 19:35:00
【问题描述】:

当我触摸这个地方时,我的带有 EditText 的 TextInputLayout 正在减少。
我有 2 个问题,如果我想要相同大小的 EditText,我应该解决什么问题,以及我应该为屏幕上的键盘添加什么。

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="pl.xd.hello.WeatherActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="6"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Podaj miejscowość" />
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:gravity="center"
            android:orientation="horizontal">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/floatingActionButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:clickable="true"
                app:backgroundTint="@android:color/holo_red_dark"
                app:fabSize="mini"
                app:srcCompat="@drawable/ic_check_circle_black_24dp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ListView
            android:id="@+id/weatherListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    对于您的第一个问题,让您的 EditText 如下所示,以使用其父大小修复它

    <android.support.design.widget.TextInputLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
    <EditText
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="Podaj miejscowość" />
    </android.support.design.widget.TextInputLayout>
    

    对于第二个问题,您应该在AndroidManifest.xml 文件中的活动中添加android:windowSoftInputMode="stateAlwaysVisible",如下所示:

    <activity android:name=".MainActivity"
     android:label="@string/app_name"
     android:windowSoftInputMode="stateAlwaysVisible" />
    

    这将为您的整个应用打开软键盘, 您可以查看this post 了解更多信息。 但如果您只想为此 activityfragment 打开键盘,您可以添加手动打开键盘的方法,如下所示:

    public static void showKeyboard(EditText editText) {
    
        if (editText != null) {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
        }
    }
    

    【讨论】:

    • 从 match_parent 更改为 wrap_content 没有帮助,触摸后编辑文本仍在减少
    • 如果我改变体重,我有更多的地方,但无论如何尺寸正在改变
    • “权重”是删减增减,没有区别,都是触摸改变大小的:(
    • srry 我没有注意到权重是针对宽度而不是针对高度的,所以请尝试让您 TextInputLayout android:layout_height="wrap_content" 看看这是否解决了您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    相关资源
    最近更新 更多