【问题标题】:How to create custom Android radio buttons?如何创建自定义 Android 单选按钮?
【发布时间】:2013-10-07 10:51:04
【问题描述】:

我的 Android 应用中有几个单选按钮选项,但我希望它们看起来完全不同。更多类似下面的内容(快速模型),您只需单击所需的字词,即可将它们变为粗体并加下划线。

有人知道我怎样才能实现这样的目标吗?欢迎所有提示!

【问题讨论】:

  • 如果你得到这个答案,请分享。这将是一个很大的帮助

标签: java android styles radio-button themes


【解决方案1】:

一般来说,要覆盖默认小部件的外观,您需要创建一个可绘制文件夹并将所有 xml 定义放在该文件夹中。然后在布局的 RadioButton 块中引用该 xml 文件。

这是一篇很好的博文,介绍了如何做到这一点:

http://blog.devminded.com/posts/custom-android-radiobutton

【讨论】:

    【解决方案2】:

    我知道可能会迟到,但这不是为自己保留解决方案的理由。

    1) 您需要为RadioGroup 实现.XML 布局,它是RadioButtons。使用Horizontal 值设置RadioGroup 子方向以并排显示按钮。使用@null 值设置RadioButton 按钮以隐藏默认选择器

    如下:

    <RadioGroup
            android:id="@+id/my_radiogroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/i_like_radiobutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:padding="10dp"
                android:text="I Like"
                android:textColor="@android:color/holo_orange_light" />
    
            <RadioButton
                android:id="@+id/i_dont_like_radiobutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:padding="10dp"
                android:text="I Dont Like"
                android:textColor="@android:color/holo_orange_light" />
        </RadioGroup>
    

    2) 在您的Activity 类中,初始化它们并设置它们的监听器。侦听器应跟踪RadioButton 更改的更改,并根据状态选择或取消选择设置 UI 更改。如下:

        RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.my_radiogroup);
        RadioButton likeRadioButton = (RadioButton) findViewById(R.id.i_like_radiobutton);
        RadioButton dontLikeRadioButton = (RadioButton) findViewById(R.id.i_dont_like_radiobutton);
    
        //Like button listener
        likeRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //Make the text underlined
                    SpannableString content = new SpannableString(getString(R.string.like_text));
                    content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
                    buttonView.setText(content);
                    //Make the text BOLD
                    buttonView.setTypeface(null, Typeface.BOLD);
                } else {
                    //Change the color here and make the Text bold
                    SpannableString content = new SpannableString(getString(R.string.like_text));
                    content.setSpan(null, 0, content.length(), 0);
                    buttonView.setText(content);
                    buttonView.setTypeface(null, Typeface.NORMAL);
    
                }
            }
        });
    
        //Don't Like button listener
        dontLikeRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //Change the color here and make the Text bold
                    SpannableString content = new SpannableString(getString(R.string.like_text));
                    content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
                    buttonView.setText(content);
                    buttonView.setTypeface(null, Typeface.BOLD);
                } else {
                    //Change the color here and make the Text bold
                    SpannableString content = new SpannableString(getString(R.string.like_text));
                    content.setSpan(null, 0, content.length(), 0); 
                 buttonView.setText(content);
                 buttonView.setTypeface(null, Typeface.NORMAL);
    
                }
            }
        });
    

    3) 现在,RadioButton 将根据其状态自动更改其颜色和 TextStyle。如果需要,您可以添加更多自定义。

    4) 为了在用户选择任何按钮时执行所需的操作,我们需要重写RadioGroupsetOnCheckedChangeListener 方法,如下所示:

    genderRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.i_dont_like_radiobutton) {
                    //Do some actions
                } else if (checkedId == R.id.i_like_radiobutton){
    
                }
            }
        });
    

    除了分隔符之外,最终输出将与问题图像非常相似。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2020-05-18
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      相关资源
      最近更新 更多