要實(shí)現(xiàn)Android MaterialCardView的焦點(diǎn)設(shè)置,可以使用以下方法:
<com.google.android.material.card.MaterialCardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
app:cardCornerRadius="8dp">
<!-- card content -->
</com.google.android.material.card.MaterialCardView>
MaterialCardView cardView = findViewById(R.id.card_view);
cardView.requestFocus();
MaterialCardView cardView = findViewById(R.id.card_view);
cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// 處理獲取焦點(diǎn)的操作
} else {
// 處理失去焦點(diǎn)的操作
}
}
});