簡體   English   中英

選中時作為選項卡突出顯示的單選按鈕(Android Studio)

[英]Radio button as tabs highlight when selected (Android Studio)

我正在嘗試創建單選按鈕,允許用戶選擇三個選項之一。 選擇后,該選項應有邊框,而其他選項則沒有。

這是我正在嘗試創建的圖像:

在此處輸入圖片說明

我不知道該怎么做。 任何建議都非常感謝。

您可以創建一組對象(它們是選項)並以編程方式確保只能選擇其中一個。 選擇可以按照您的意願呈現,我將給出一個代碼示例,該示例大致模仿您附加的圖像。 這是結構:

XML 文件:具有不可見 FrameLayout 基礎的 CardViews(將用作筆划),基礎上方是卡片的內容

Java 類: setChecked(MaterialCardView selected)方法選擇所選卡片(使其“中風”可見)和checkedCard()返回所選卡片

所以這是代碼:

活動_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/parentBackground"
    tools:context=".MainActivity">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/public_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:cardBackgroundColor="@color/cardBackground"
        app:cardCornerRadius="5dp">

        <FrameLayout
            android:id="@+id/public_stroke"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/selectedCardStroke"
            android:visibility="visible" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:background="@color/cardBackground">

            <com.github.abdularis.civ.CircleImageView
                android:id="@+id/card1Icon"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_centerVertical="true"
                android:layout_margin="20dp"
                android:src="@mipmap/ic_public" />

            <TextView
                android:id="@+id/card1Label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@id/card1Icon"
                android:text="Public Profile"
                android:textAllCaps="false"
                android:textColor="@color/cardLabelColor"
                android:textSize="35sp" />

            <TextView
                android:id="@+id/card1Text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/card1Label"
                android:layout_alignStart="@id/card1Label"
                android:text="Everyone can see your account. \nIncluding non followers."
                android:textAllCaps="false"
                android:textColor="@color/cardTextColor"
                android:textSize="15sp" />

        </RelativeLayout>

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/friends_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/public_card"
        android:layout_margin="10dp"
        app:cardBackgroundColor="@color/cardBackground"
        app:cardCornerRadius="5dp">

        <FrameLayout
            android:id="@+id/friends_stroke"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/selectedCardStroke"
            android:visibility="invisible" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:background="@color/cardBackground">

            <com.github.abdularis.civ.CircleImageView
                android:id="@+id/card2Icon"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_centerVertical="true"
                android:layout_margin="20dp"
                android:src="@mipmap/ic_friends" />

            <TextView
                android:id="@+id/card2Label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@id/card2Icon"
                android:text="Friends Only"
                android:textAllCaps="false"
                android:textColor="@color/cardLabelColor"
                android:textSize="35sp" />

            <TextView
                android:id="@+id/card2Text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/card2Label"
                android:layout_alignStart="@id/card2Label"
                android:text="Connections nly. Only your friends \nand friends of friends can see your account."
                android:textAllCaps="false"
                android:textColor="@color/cardTextColor"
                android:textSize="15sp" />

        </RelativeLayout>

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/private_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/friends_card"
        android:layout_margin="10dp"
        app:cardBackgroundColor="@color/cardBackground"
        app:cardCornerRadius="5dp">

        <FrameLayout
            android:id="@+id/private_stroke"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/selectedCardStroke"
            android:visibility="invisible" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:background="@color/cardBackground">

            <com.github.abdularis.civ.CircleImageView
                android:id="@+id/card3Icon"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_centerVertical="true"
                android:layout_margin="20dp"
                android:src="@mipmap/ic_private" />

            <TextView
                android:id="@+id/card3Label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@id/card3Icon"
                android:text="Private"
                android:textAllCaps="false"
                android:textColor="@color/cardLabelColor"
                android:textSize="35sp" />

            <TextView
                android:id="@+id/card3Text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/card3Label"
                android:layout_alignStart="@id/card3Label"
                android:text="Only you can see your account."
                android:textAllCaps="false"
                android:textColor="@color/cardTextColor"
                android:textSize="15sp" />

        </RelativeLayout>

    </com.google.android.material.card.MaterialCardView>
</RelativeLayout>

主活動.java:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.google.android.material.card.MaterialCardView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public MaterialCardView cPublic, cFriends, cPrivate;
    public FrameLayout sPublic, sFriends, sPrivate;
    public ArrayList<MaterialCardView> cards;
    public ArrayList<FrameLayout> strokes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cards = new ArrayList<>();
        strokes = new ArrayList<>();

        cPublic = findViewById(R.id.public_card);
        cFriends = findViewById(R.id.friends_card);
        cPrivate = findViewById(R.id.private_card);

        cards.add(cPublic);
        cards.add(cFriends);
        cards.add(cPrivate);

        sPublic = findViewById(R.id.public_stroke);
        sFriends = findViewById(R.id.friends_stroke);
        sPrivate = findViewById(R.id.private_stroke);

        strokes.add(sPublic);
        strokes.add(sFriends);
        strokes.add(sPrivate);

        cPublic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChecked(cPublic);
            }
        });

        cFriends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChecked(cFriends);
            }
        });

        cPrivate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChecked(cPrivate);
            }
        });

    }

    public void setChecked(MaterialCardView selected) {

        int index = cards.indexOf(selected);
        FrameLayout stroke = strokes.get(index);

        stroke.setVisibility(View.VISIBLE);

        for (FrameLayout s : strokes) {
            if (!s.equals(stroke)) {
                s.setVisibility(View.INVISIBLE);
            }
        }

    }

    public MaterialCardView checkedCard() {
        int index = 0;
        for (FrameLayout s : strokes) {
            if (s.getVisibility() == View.VISIBLE) {
                index = strokes.indexOf(s);
            }
        }
        return cards.get(index);
    }
}

希望可以幫到你(:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM