[英]Radio button as tabs highlight when selected (Android Studio)
I am trying to create radio buttons that allows the users to select one of the three options.我正在尝试创建单选按钮,允许用户选择三个选项之一。 Once selected, the option should have a border, while the others do not.
选择后,该选项应有边框,而其他选项则没有。
Here is an image of what I am trying to create:这是我正在尝试创建的图像:
I'm not sure how to go about this.我不知道该怎么做。 Any suggestions are greatly appreciated.
任何建议都非常感谢。
You could create a group of objects (which are the options) and programmatically ensure that only one of them can be selected.您可以创建一组对象(它们是选项)并以编程方式确保只能选择其中一个。 selection could be presented as you wish it would be, I'd give a code example that imitates approximately the image you've attached.
选择可以按照您的意愿呈现,我将给出一个代码示例,该示例大致模仿您附加的图像。 here's the structure:
这是结构:
XML file: CardViews with base of invisible FrameLayout (will be functioning as stroke), above the base is the cards' content XML 文件:具有不可见 FrameLayout 基础的 CardViews(将用作笔划),基础上方是卡片的内容
Java Class: setChecked(MaterialCardView selected) method that selects the chosen card (making it's 'stroke' visible) and checkedCard() that returns the selected card Java 类: setChecked(MaterialCardView selected)方法选择所选卡片(使其“中风”可见)和checkedCard()返回所选卡片
so here's the code:所以这是代码:
activity_main.xml:活动_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:主活动.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);
}
}
Hope it could help you (:希望可以帮到你(:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.