我想知道如何將筆觸顏色應用于 onClick 的卡片視圖,并在再次單擊時取消選擇它
另外,我有多個卡片視圖,所以如果已經選擇了一個并且應用了筆觸顏色,但是如果用戶單擊另一個卡片視圖,則筆觸顏色從第一個卡片視圖消失并應用于受尊重的卡片視圖
我怎樣才能做到這一點?
現在我已將描邊顏色應用于一個卡片視圖
進一步說明,我想在CardView單擊捐贈按鈕時獲取選定的 ID 或金額,并將卡視圖(金額)傳遞給支付網關。
這是它的樣子:

donate_fragment.xml // 只有卡片視圖部分和按鈕
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="@string/select_an_amount"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold|normal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<com.google.android.material.card.MaterialCardView
android:layout_width="100dp"
android:layout_height="80dp"
app:strokeColor="@color/wallet_holo_blue_light"
app:strokeWidth="3dp"
android:layout_marginEnd="10dp"
android:backgroundTint="@color/Card_Elevation_Color"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="10dp"
app:cardElevation="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/_0_5"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold|normal" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginEnd="10dp"
android:backgroundTint="@color/Card_Elevation_Color"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="10dp"
app:cardElevation="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/_1"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold|normal" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="100dp"
android:layout_height="80dp"
android:backgroundTint="@color/Card_Elevation_Color"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="10dp"
app:cardElevation="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/_5"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold|normal" />
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<com.google.android.material.card.MaterialCardView
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginEnd="10dp"
android:backgroundTint="@color/Card_Elevation_Color"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="10dp"
app:cardElevation="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/_10"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold|normal" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginEnd="10dp"
android:backgroundTint="@color/Card_Elevation_Color"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="10dp"
app:cardElevation="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/_50"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold|normal" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="100dp"
android:layout_height="80dp"
android:backgroundTint="@color/Card_Elevation_Color"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="10dp"
app:cardElevation="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/_100"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold|normal" />
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@ id/donateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="45dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="45dp"
android:text="@string/donate"
android:textAlignment="center"
android:textSize="17sp"
app:cornerRadius="10dp" />
實施答案后,這是結果//謝謝@Zain
關聯
更新// 如何獲取選中的卡片視圖
// Donate Btn
donateBtn=view.findViewById(R.id.donateBtn);
donateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
這是單擊捐贈箱時我要呼叫的方法
private Object makePayment() {
Checkout checkout = new Checkout();
checkout.setKeyID("<Key>");
checkout.setImage(R.drawable.ic_baseline_favorite_24);
final Donate activity = this;
try {
JSONObject options = new JSONObject();
options.put("name", "Indie.Dev");
options.put("description", "Reference No. #123456");
options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
options.put("order_id", "order_DBJOWzybf0sJbb");//from response of step 3.
options.put("theme.color", "#3399cc");
options.put("currency", "USD");
options.put("amount", "0.5");//pass amount in currency subunits
options.put("prefill.email", "[email protected]");
options.put("prefill.contact", "9859461607");
JSONObject retryObj = new JSONObject();
retryObj.put("enabled", true);
retryObj.put("max_count", 4);
options.put("retry", retryObj);
checkout.open((Activity) requireContext(), options);
} catch (Exception e) {
Log.e("TAG", "Error in starting Razorpay Checkout", e);
}
return null;
}
uj5u.com熱心網友回復:
您可以使用描邊顏色的選擇器來更改state_checked:
選擇器.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#36B3E0" android:state_checked="true" />
<item android:color="@android:color/transparent" android:state_checked="false" />
</selector>
將其應用于CardView:
app:checkedIcon="@null"
app:strokeColor="@drawable/selector"
android:checkable="true"
checkedIcon設定為沒有勾選已選中卡片的圖示。
然后更改檢查狀態OnClickListener
但在此之前通過使用它們的周圍清除所有檢查過的卡片ViewGroup,這是一個擴展功能:
fun ViewGroup.clearCardSelection() {
this.children.forEach {
if (it is MaterialCardView)
it.isChecked = false
}
}
和聽眾:
val listener = View.OnClickListener {
if (it is MaterialCardView) {
val isChecked = it.isChecked
linearlayout1.clearCardSelection() // top LinearLayout
linearlayout2.clearCardSelection() // bottom LinearLayout
it.isChecked = !isChecked
}
}
cardView.setOnClickListener(listener) // repeat this for the 6 cards
這是結果:

確保永久保存已檢查的卡狀態,以免在配置更改期間丟失。
更新
這是java等價物:
private void clearCardSelection(LinearLayout layout) {
for (int i = 0; i < layout.getChildCount(); i ) {
View child = layout.getChildAt(i);
if (child instanceof MaterialCardView)
((MaterialCardView) child).setChecked(false);
}
}
View.OnClickListener listener = view -> {
if (view instanceof MaterialCardView) {
MaterialCardView card = (MaterialCardView) view;
boolean isChecked = card.isChecked();
clearCardSelection(linearlayout1);
clearCardSelection(linearlayout2);
card.setChecked(!isChecked);
}
};
cardView.setOnClickListener(listener); // repeat for all the cards
當我在java中測驗它時;我在MaterialCaredView布局上得到了 NPE,那是因為設定app:checkedIcon="@null"為 null,要解決此問題,請替換app:checkedIcon="@null"為app:checkedIcon="@android:color/transparent"
uj5u.com熱心網友回復:
好的,我已經解決了這樣的第二個問題并且它有效
好的,我知道這是實作 if() 的一種非常糟糕的方法,但是如果您有更好的解決方案,請更新此答案
// Donate Btn
donateBtn = view.findViewById(R.id.donateBtn);
donateBtn.setOnClickListener(view12 -> {
if (cardView1.isChecked()) {
makePayment1();
}
if (cardView2.isChecked()) {
makePayment2();
}
if (cardView3.isChecked()) {
makePayment3();
}
if (cardView4.isChecked()) {
makePayment4();
}
if (cardView5.isChecked()) {
makePayment5();
}
if (cardView6.isChecked()) {
makePayment6();
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/426320.html
