android - 编辑文本的最后状态仍然显示为某种阴影?

标签 android android-edittext

嗯,这很奇怪,所以,我有一个控制权。带有 RelativeLayout 和一些其他控件的简单 CardView。其中之一是 EditText。基本上,我根据 EditText 的焦点变化执行了一些不同的操作。

  1. 显示/隐藏“X”按钮,用于清除编辑文本。 (在执行 #1 时,我在 X 广告搜索图标之间交换 EditTexttoLeftOf 属性 - 从而自动缩小/增大 EditText)<
  2. 添加/删除提示文本,在本例中为“点击搜索”文本。

奇怪的是,在我完成之后,仍然有一条过去状态的痕迹,仍然打印出来。

这是未聚焦的 EditText enter image description here

这是重点 enter image description here

请注意,X 下方有“点击搜索”和“预收缩”EditText 行的阴影。

这是我与 EditText

相关的 Java fragment
    queryStringEText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b) {
                clearIButton.setVisibility(VISIBLE);
                ((RelativeLayout.LayoutParams) queryStringEText.getLayoutParams()).
                        addRule(RelativeLayout.LEFT_OF, clearIButton.getId());

                //Hide hint text
                queryStringEText.setHint("");

                //Show the keyboard
                ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).
                        showSoftInput(queryStringEText, InputMethodManager.SHOW_FORCED);
            } else {
                clearIButton.setVisibility(GONE);
                ((RelativeLayout.LayoutParams) queryStringEText.getLayoutParams()).
                        addRule(RelativeLayout.LEFT_OF, searchIButton.getId());

                //Hide hint text
                queryStringEText.setHint("Tap to search");

                //Hide the keyboard
                ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).
                        hideSoftInputFromWindow(queryStringEText.getWindowToken(), 0);
            }
        }
    });

这是 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView     
    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:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#80000000"
    app:cardCornerRadius="0dp"
    app:cardPreventCornerOverlap="false">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageButton
        android:id="@+id/addon_search_panel_search"
        style="@style/FoodMap_ImageButtonWithNoBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="?android:attr/selectableItemBackground"
        android:padding="@dimen/default_content_padding"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_search_white_24dp" />

    <ImageButton
        android:id="@+id/addon_search_panel_clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/addon_search_panel_search"
        android:background="?android:attr/selectableItemBackground"
        android:paddingBottom="@dimen/default_content_padding"
        android:paddingTop="@dimen/default_content_padding"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_close_yellow_24dp"
        android:visibility="gone" />

    <EditText
        android:id="@+id/addon_search_panel_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/addon_search_panel_search"
        android:hint="Tap to search"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:maxLines="1"
        android:textColor="@android:color/white"
        android:textColorHint="#aaa"
        app:backgroundTint="@color/FoodMap_colorAccent"
        tools:ignore="MissingPrefix" />

</RelativeLayout>

如何去除那些“过去的影子”?

附言每次焦点更改后尝试 querySearchEText.requestLayout() 但没有任何反应。

附言即使在我输入一些文本后,“点击搜索”阴影仍然存在。

最佳答案

您是否尝试过为父级请求布局而不是 .getParent().requestLayout();

尽管最简单的解决方案是将两个 ImageButton 放入 LinearLayout 并将您的 EditText 放置在该布局的左侧。

编辑:

 queryStringEText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b) {
                clearIButton.setVisibility(VISIBLE);

                //Hide hint text
                queryStringEText.setHint("");

                //Show the keyboard
                ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).
                        showSoftInput(queryStringEText, InputMethodManager.SHOW_FORCED);
            } else {
                clearIButton.setVisibility(GONE);

                //Hide hint text
                queryStringEText.setHint("Tap to search");

                //Hide the keyboard
                ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).
                        hideSoftInputFromWindow(queryStringEText.getWindowToken(), 0);
            }
        }
    });

和 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    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:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#80000000"
    app:cardCornerRadius="0dp"
    app:cardPreventCornerOverlap="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:id="@+id/controls_layout">

            <ImageButton
                android:id="@+id/addon_search_panel_clear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:paddingBottom="@dimen/default_content_padding"
                android:paddingTop="@dimen/default_content_padding"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_close_yellow_24dp"
                android:visibility="gone" />

            <ImageButton
                android:id="@+id/addon_search_panel_search"
                style="@style/FoodMap_ImageButtonWithNoBackground"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?android:attr/selectableItemBackground"
                android:padding="@dimen/default_content_padding"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_search_white_24dp" />

        </LinearLayout>

        <EditText
            android:id="@+id/addon_search_panel_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/controls_layout"
            android:hint="Tap to search"
            android:imeOptions="actionSearch"
            android:inputType="text"
            android:maxLines="1"
            android:textColor="@android:color/white"
            android:textColorHint="#aaa"
            app:backgroundTint="@color/FoodMap_colorAccent"
            tools:ignore="MissingPrefix" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

关于android - 编辑文本的最后状态仍然显示为某种阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765572/

相关文章:

android - 在android上调整位图大小的最有效内存方法?

Android RoleManager删除自己包的角色RoleManager.ROLE_SMS

android - 在 recyclerview 中提交列表后强制编辑文本以移除焦点

Android 错误与 Edittext MaxLines

java - 按下完成后,Eclipse editText 移除焦点(光标)

Android:文本缩放到最大可能而不换行

java - RALLY Android 应用程序的用户登录流程

java - 使用简单 XML 的 XML 反序列化错误

android - 部分不可编辑的多行编辑文本,例如填空

java - 用户可拖动的编辑文本