私はクイズベースのアプリを開発しています。1 つの質問と 4 つのオプション (ラジオ ボタン) があります。ユーザーが間違った答えを選択した場合、そのラジオボタンの色を赤に変えたいと思います。これを行う方法?
11 に答える
これで本当に役立つものを見せに来ました:
誰もがティントの使い方とカラーアクセントの使い方について話していますが、これは 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>
一番手っ取り早いのは、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>
結果:
@smashingが指摘したように、これはAPIレベル> = 21でのみ機能します
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>
ラジオボタンで下位互換性のある色合いを実行できます
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));
このサイトは、Android コンポーネント全般のカスタマイズに非常に適しています: android-holo-colors
ラジオ ボタンを選択し、色を赤にして、ダウンロードしてプロジェクトで使用するだけです。
コトリンユーザー向け
拡張機能を作成する
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()
終わり
こんな感じで画像を作成!描画可能なフォルダーに配置します..それを呼び出して、
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');
}
});
}