【问题标题】:Radio button as tabs highlight when selected (Android Studio)选中时作为选项卡突出显示的单选按钮(Android Studio)
【发布时间】:2020-01-04 23:29:31
【问题描述】:

我正在尝试创建允许用户选择三个选项之一的单选按钮。选择后,该选项应有边框,而其他选项则没有。

这是我正在尝试创建的图像:

我不知道该怎么做。非常感谢任何建议。

【问题讨论】:

    标签: java android xml android-studio


    【解决方案1】:

    您可以创建一组对象(它们是选项)并以编程方式确保只能选择其中一个。 选择可以按照您的意愿呈现,我将给出一个代码示例,该示例大致模仿您附加的图像。这是结构:

    XML 文件: 带有不可见 FrameLayout 基础的 CardViews(将用作笔划),基础之上是卡片的内容

    Java 类: setChecked(MaterialCardView selected) 方法选择所选卡片(使其“笔触”可见)和 checkedCard()返回所选卡片

    代码如下:

    activity_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>
    

    MainActivity.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);
        }
    }
    

    希望对你有帮助(:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      相关资源
      最近更新 更多