嗯,这很奇怪,所以,我有一个控制权。带有 RelativeLayout
和一些其他控件的简单 CardView
。其中之一是 EditText
。基本上,我根据 EditText
的焦点变化执行了一些不同的操作。
- 显示/隐藏“X”按钮,用于清除编辑文本。 (在执行 #1 时,我在 X 广告搜索图标之间交换
EditText
的toLeftOf
属性 - 从而自动缩小/增大EditText
)< - 添加/删除提示文本,在本例中为“点击搜索”文本。
奇怪的是,在我完成之后,仍然有一条过去状态的痕迹,仍然打印出来。
请注意,X 下方有“点击搜索”和“预收缩”EditText
行的阴影。
这是我与 EditText
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/