18

私はクイズベースのアプリを開発しています。1 つの質問と 4 つのオプション (ラジオ ボタン) があります。ユーザーが間違った答えを選択した場合、そのラジオボタンの色を赤に変えたいと思います。これを行う方法?

4

11 に答える 11

23

これで本当に役立つものを見せに来ました:

誰もがティントの使い方とカラーアクセントの使い方について話していますが、これは API が 21 未満の電話では機能しません。

したがって、これに関する本当の修正、または少なくとも私を助けたのは、android.support.v7.widget.AppCompatRadioButton代わりに使用することでしたRadioButton

これをレイアウトで使用すると、次を使用できます。 app:buttonTint="@color/yourColor"

ビューの互換性に関する警告や問題は発生しません。

そして、追加することを忘れないでください:

xmlns:app="http://schemas.android.com/apk/res-auto"

レイアウトの親またはウィジェットに。

編集

@aselims はコメントで名前空間にないことを述べてbuttonTintいます。app

だから...これがこのソリューションの私の現在のスタイルです:

<style name="MRadioButton.Purple" parent="Widget.AppCompat.CompoundButton.RadioButton">
    <item name="colorAccent">@color/youColor</item>
    <item name="colorControlHighlight">@color/yourColor</item>
    <item name="android:colorPressedHighlight">@color/yourColor</item>
    <item name="colorPrimaryDark">@color/yourColor</item>
    <item name="colorPrimary">@color/yourColor</item>
    <item name="colorControlActivated">@color/yourColor</item>
</style>
于 2016-02-24T18:48:03.960 に答える
17

一番手っ取り早いのは、buttonTintを目的の色に設定することです。

 <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radio"
        android:checked="true"
        android:buttonTint="@color/your_color"/>

この場合、あなたvalues/colors.xmlの色を赤みを帯びたものに入れます:

<color name="your_color">#e75748</color>

結果:

色付きの Android ラジオ ボタン

@smashingが指摘したように、これはAPIレベル> = 21でのみ機能します

于 2015-04-09T23:26:17.207 に答える
8

RadioButton ボタンの色をプログラムで変更し、API レベル < 21 で動作するようにするAppCompatRadioButtonには、代わりにRadioButton次を使用する必要があります。

(それ以外の場合は警告しますsetbuttontintlist requrie api level 21)

import android.support.v7.widget.AppCompatRadioButton;

AppCompatRadioButton radioButton = new AppCompatRadioButton(getActivity());
radioButton.setSupportButtonTintList(
        ContextCompat.getColorStateList(getActivity(),
        R.color.single_choice_state_list));

single_choice_state_list.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/single_choice_checked"></item>
    <item android:state_checked="false" android:color="@color/single_choice_unchecked"></item>
</selector>
于 2016-03-01T07:58:30.407 に答える
4

ラジオボタンで下位互換性のある色合いを実行できます

XML:

<android.support.v7.widget.AppCompatRadioButton
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/radioButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:buttonTint="@color/red"/>

またはジャバ:

CompoundButton button = (CompoundButton) findViewById(R.id.radioButton);
CompoundButtonCompat.setButtonTintList(button, ContextCompat.getColorStateList(this, R.color.red));
于 2017-07-13T20:50:57.990 に答える
4

このサイトは、Android コンポーネント全般のカスタマイズに非常に適しています: android-holo-colors

ラジオ ボタンを選択し、色を赤にして、ダウンロードしてプロジェクトで使用するだけです。

于 2013-09-09T10:37:44.837 に答える
1

コトリンユーザー向け

拡張機能を作成する

fun RadioButton.setCircleColor() {
    val colorStateList = ColorStateList(
        arrayOf(
            intArrayOf(-android.R.attr.state_checked), // unchecked
            intArrayOf(android.R.attr.state_checked)  // checked
        ), intArrayOf(
           Color.RED, // unchecked color
           Color.GREEN  // checked color
        )
    )

    // finally set button tint list
    buttonTintList = colorStateList

    // optionally tint mode or tint blend
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
        buttonTintBlendMode = BlendMode.SRC_IN
    }else{
        buttonTintMode = PorterDuff.Mode.SRC_IN
    }

    invalidate() // could not be necessary
}

今それを呼び出します

   radioButton.setCircleColor()

終わり

于 2020-11-08T09:59:00.233 に答える
0

こんな感じで画像を作成!描画可能なフォルダーに配置します..それを呼び出して、

 RadioButton rb=(RadioButton) findViewById(R.id.radioButton1);
        rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                rb.setButtonDrawable(R.drawable.'you image here');
            }
        });
    }
于 2013-09-09T11:01:31.160 に答える