要實現Android MaterialCardView的焦點設置,可以使用以下方法:
<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) {
// 處理獲取焦點的操作
} else {
// 處理失去焦點的操作
}
}
});