我尝试在textInputLayout上设置setOnFocusChangeListener。API在28以上运行良好。此外,setOnClickListener在28以上的API上不起作用。如何在API higher 28上修复它?我的代码:
with(binding) {
textFieldSearch.setOnFocusChangeListener { v, hasFocus ->
Log.d(javaClass.simpleName,"SetOn: Focus")
if (hasFocus) {
textFieldSearch.setStartIconDrawable(android.R.drawable.ic_menu_search)
tabLayout.visibility = View.VISIBLE
viewpager.visibility = View.VISIBLE
popularSearchLay.container.visibility = View.GONE
} else {
textFieldSearch.setStartIconDrawable(R.drawable.ic_arrow_arrows_back)
tabLayout.visibility = View.GONE
viewpager.visibility = View.GONE
popularSearchLay.container.visibility = View.VISIBLE
}
}
}
我的布局:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textFieldSearch"
style="@style/CustomTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_scrollFlags="scroll|enterAlways">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/textInputSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="@dimen/edit_text_hint_padding"
android:imeOptions="actionSearch"
android:inputType="textFilter" />
</com.google.android.material.textfield.TextInputLayout>
MyStyle:
<style name="CustomTextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxCornerRadiusTopStart">@dimen/searchViewCornerRadius</item>
<item name="boxCornerRadiusBottomEnd">@dimen/searchViewCornerRadius</item>
<item name="boxCornerRadiusTopEnd">@dimen/searchViewCornerRadius</item>
<item name="boxCornerRadiusBottomStart">@dimen/searchViewCornerRadius</item>
<item name="borderWidth">@dimen/searchViewBorderWidth</item>
<item name="boxStrokeColor">@color/black</item>
<item name="android:layout_margin">@dimen/textFieldMargin</item>
<item name="android:hint">@string/hint_edit_text</item>
<item name="hintTextColor">@color/black</item>
<item name="endIconMode">clear_text</item>
<item name="startIconDrawable">@android:drawable/ic_menu_search</item>
</style>
textinputSearch.setOnFocusChangeListener { v, hasFocus ->
Log.d(javaClass.simpleName,"SetOn: Focus")
if (!hasFocus) {
textinputSearch.setStartIconDrawable(android.R.drawable.ic_menu_search)
tabLayout.visibility = View.VISIBLE
viewpager.visibility = View.VISIBLE
popularSearchLay.container.visibility = View.GONE
} else {
textFieldSearch.setStartIconDrawable(R.drawable.ic_arrow_arrows_back)
tabLayout.visibility = View.GONE
viewpager.visibility = View.GONE
popularSearchLay.container.visibility = View.VISIBLE
}
}
在您的文本输入中使用样式
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"