如何使图像按钮的行为类似于单选按钮

huangapple go评论80阅读模式
英文:

How to make Image Buttons act like Radio buttons

问题

我正在尝试使图像按钮的行为类似于单选按钮(Radio buttons)。让我解释一下。我有一个基本布局,其中包含一个图像按钮(Image Button)和一个文本视图(TextView),我会加载不同的图像和文本。

XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/button_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFFFF"/>

    <TextView
        android:id="@+id/text_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

</LinearLayout>

Java方法:

LinearLayout categoryLayout = view.findViewById(R.id.categories_layout);

for (int i = 0; i<categories.size(); i++) {
    final String name = categories.get(i).name;
    final int resource = categories.get(i).resource;

    View v = getLayoutInflater().inflate(R.layout.choose_category, null);

    final TextView text = v.findViewById(R.id.text_category);
    text.setText(name);

    ImageButton button = v.findViewById(R.id.button_category);
    button.setImageResource(resource);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectedCategory = resource;
            text.setTypeface(text.getTypeface(), Typeface.BOLD);
        }
    });

    categoryLayout.addView(v);
}

每当单击图像时,文本将设置为粗体,以指示单击了哪个按钮。我的问题是,我只想一次只能点击一个按钮。我考虑过,每次单击按钮时,重置所有文本视图的外观,只保留上次单击的文本为粗体。然而,我不知道如何遍历生成的所有布局。

我希望我表达清楚了,如果你能帮助我,谢谢!

英文:

I'm trying to make Image buttons act like Radio buttons. Let me explain. I have a basic layout containing an Image Button and a TextView, that I load with different images and texts.

XML layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/button_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFFFF"/>

    <TextView
        android:id="@+id/text_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

</LinearLayout>

Java method :

LinearLayout categoryLayout = view.findViewById(R.id.categories_layout);

        for (int i = 0; i<categories.size(); i++) {
            final String name = categories.get(i).name;
            final int resource = categories.get(i).resource;

            View v = getLayoutInflater().inflate(R.layout.choose_category, null);

            final TextView text = v.findViewById(R.id.text_category);
            text.setText(name);

            ImageButton button = v.findViewById(R.id.button_category);
            button.setImageResource(resource);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    selectedCategory = resource;
                    text.setTypeface(text.getTypeface(), Typeface.BOLD);
                }
            });

            categoryLayout.addView(v);
        }

Every time an image is clicked, the text is set bold to indicate which button was clicked. My problem is that I only want one button to be clickable at a time. I thought of, each time a button is clicked, reseting the appearance of all TextView, only leaving the last text that was clicked as bold. However, I don't know how to navigate through all layouts that have been generated.

I hope I was clear, thank you if you can help me !

答案1

得分: 0

这可能不是解决这个问题最有效的方法,但它可以工作。每次单击按钮时,所选的图标都会被保存。然后,我在RadioGroup上使用removeAllViews()方法,重新创建视图。我只需确保先前所选图标下方的文本设置为粗体。

英文:

That might not be the most efficient way to solve this problem, but it works. Every time the button is clicked, the icon selected is saved. Then, I use removeAllViews() method on the RadioGroup, and recreate the views. I just make sure the text below the previously selected icon is set bold.

huangapple
  • 本文由 发表于 2020年10月21日 04:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64453070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定