【问题标题】:How to remove focus from TextInputLayout when page loads?页面加载时如何从 TextInputLayout 移除焦点?
【发布时间】:2016-02-22 16:14:25
【问题描述】:

当这个 xml 加载到一个片段中时,焦点直接转到第二个编辑文本,我希望编辑文本应该专注于触摸它。我想用我自己的颜色来提示和输入我在上面输入的文本。非常感谢任何帮助。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="23dp">

    <TextView
        android:id="@+id/basic_details_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/basic_details"
        android:textSize="16sp"
        android:paddingLeft="0dp"
        android:textColor="@color/basic_details_gray"
        android:textStyle="normal"
        android:typeface="monospace"
        android:includeFontPadding="false"
        android:layout_alignParentLeft="true"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/container_first"
        android:layout_below="@+id/basic_details_heading"
        android:paddingLeft="0dp"
        >

      <!--  <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/profile_created_for"
            android:id="@+id/sub_head_one"
            android:textStyle="normal"
            android:textColor="@color/login_background"
            android:textSize="12sp"
            android:paddingLeft="0dp"
            android:paddingTop="0dp"
            android:visibility="gone"
            android:includeFontPadding="false"
            android:layout_alignParentLeft="true"
            />-->
        <android.support.design.widget.TextInputLayout
            android:id="@+id/profile_created_for"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <EditText
                android:id="@+id/edit_text_profile_created_for"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-4dp"
                android:textSize="15sp"
                android:textColor="@color/black"
                android:textStyle="normal"
                android:hint="Profile Created For"
                android:focusable="false"/>

        </android.support.design.widget.TextInputLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nav_next"
            android:layout_alignParentRight="true"
            android:paddingTop="20dp"
            android:id="@+id/profile_created_for_arrow"
            />


        <android.support.design.widget.TextInputLayout
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/profile_created_for"
            android:focusableInTouchMode="true"
            >

            <EditText
                android:id="@+id/edit_text_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-4dp"
                android:textSize="15sp"
                android:textColor="@color/black"
                android:textStyle="normal"
                android:hint="Name"
                />

        </android.support.design.widget.TextInputLayout>


</RelativeLayout>


</RelativeLayout>

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个:在你的 xml 中添加一个虚拟对象,就在你的文本布局之前,这样它就可以消耗焦点。

    <LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0px" 
    android:layout_height="0px"/>
    

    【讨论】:

    • 嘿乔希,这将隐藏软键盘,但焦点仍然只在那里。我也无法改变颜色。
    • 很高兴它起作用了@NaveenShriyan,它还帮助我达到了 1K 点 XD
    • 我把android:focusable="true"android:focusableInTouchMode="true"放在了父布局上;效果很好。
    • 谢谢,我从过去 2 天开始一​​直在寻找这个
    【解决方案2】:

    在TextInputLayout的父视图中添加属性android:focusableInTouchMode="true"如下

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true">
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ............>
            <android.support.v7.widget.AppCompatEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                ............/>
        </android.support.design.widget.TextInputLayout>
    </RelativeLayout>
    

    【讨论】:

    • 工作就像一个魅力!谢谢;)
    【解决方案3】:

    在你的根视图(TextInputLayout)中使用这个属性:(你想禁用它)

    android:focusableInTouchMode="true"
    

    【讨论】:

    • 但是键盘还是弹出来,如何防止?
    【解决方案4】:

    我尝试在您不想自动对焦的 Edittext 之前添加另一个 EditText,它可以工作

    <EditText
          android:layout_width="0dp"
          android:layout_height="0dp" />
        <android.support.design.widget.TextInputLayout
            android:id="@+id/NghiPhep_TextInputLayout_GhiChu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:hintTextAppearance="@style/TextHintLabel"
            android:layout_marginTop="5dp"
            android:focusableInTouchMode="true"
            >
            <EditText
                android:id="@+id/NghiPhep_txt_GhiChu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/colorPrimaryDark"
                android:hint="Ghi chú"
                android:fontFamily="@font/arial_rounded"
                android:inputType="textMultiLine"
                android:singleLine="false"
                android:textSize="18sp"/>
        </android.support.design.widget.TextInputLayout>
    

    【讨论】:

    • 欢迎来到 Stack Overflow!您能否添加一些关于代码 sn-p 为何以及如何准确地提供问题答案的解释?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2021-04-06
    • 2021-10-26
    • 2016-11-19
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    相关资源
    最近更新 更多