目的是为了清除TextView的焦点,单纯clearFocus的话是没有效果的,首先在TextView的父布局上设置 :
android:focusable="true"
android:focusableInTouchMode="true"
<RelativeLayout
android:id="@+id/relativelayout_contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/contact_wireless_item_layout_margin_top"
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="@dimen/contact_wireless_item_name_margin_start"
android:layout_toStartOf="@id/linearlayout_menu"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@{contact.contactName}"
android:textSize="@dimen/contact_common_text_size">
</TextView>
</RelativeLayout>
然后在代码中,配合父布局requestFocus来清除子布局的焦点:
if (position == selectItemPosition) {
binding.root.setBackgroundColor(
ContextCompat.getColor(
mContext!!,
R.color.color_wireless_item_selected_color
)
)
//目标代码
binding.relativelayoutContactName.clearFocus()
binding.contactName.requestFocus()
} else {
binding.root.setBackgroundColor(
ContextCompat.getColor(
mContext!!,
R.color.color_wireless_item_common_color
)
)
//目标代码
binding.contactName.clearFocus()
binding.relativelayoutContactName.requestFocus()
}

当在Android代码中调用TextView的clearFocus方法无效时,可以尝试将TextView的父布局设置为focusable和focusableInTouchMode均为true,接着在代码中通过父布局的requestFocus来实现焦点清除。

929

被折叠的 条评论
为什么被折叠?



