実現したいこと
ここに実現したいことを箇条書きで書いてください。
- kotlinでCheckBoxの非活性にチェックボックスの背景色を非活性が分かりやすいようにグレーなど変更したい
前提
アンドロイドでとある条件の場合、チェックボックスを非活性にするのですが、
非活性にはなりますが、背景色が変わらないため、非活性か活性かの判断がつきにくいので
非活性の場合は
とある条件とは以下のようなチェックボックスがある場合、全てをチェックすると
全て以外のチェックボックスを非活性に仕様としています。
□全て
□条件1
□条件2
□条件3
↓
☑全て
▦条件1
▦条件2
▦条件3
発生している問題・エラーメッセージ
チェックが付いているものはわかりやすいのですが
チェックがついていないものは非活性が判断付きにくいため
チェックボックスの背景色がグレーになればと思っています。
該当のソースコード
setting_fragment.xml
kotlin
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".SettingFragment"> 8 9 <!-- 画面をスクロールさせる --> 10 <ScrollView 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" 13 android:layout_marginTop="16dp" 14 android:layout_marginStart="16dp" 15 android:id="@+id/scrollView"> 16 17 <!-- スクロールさせたいTextViewを複数設定したいため、LinearLayoutとしてまとめる --> 18 <LinearLayout 19 android:id="@+id/LinearLayout" 20 android:orientation="vertical" 21 android:layout_height="wrap_content" 22 android:layout_width="fill_parent"> 23 24 <!-- クラスのチェックボックス --> 25 <TextView 26 android:id="@+id/class_textview" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="@string/classname" 30 android:textSize="20sp" 31 android:textColor="@color/grey_600" 32 /> 33 <CheckBox 34 android:id="@+id/class_checkbox_all" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="全て" 38 android:layout_marginStart="20dp" 39 android:textColor="@color/grey_600" 40 /> 41 <CheckBox 42 android:id="@+id/class_checkbox1" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:text="@string/class01" 46 android:layout_marginStart="20dp" 47 android:textColor="@color/grey_600" 48 /> 49 <CheckBox 50 android:id="@+id/class_checkbox2" 51 android:layout_width="wrap_content" 52 android:layout_height="wrap_content" 53 android:text="@string/class02" 54 android:layout_marginStart="20dp" 55 android:textColor="@color/grey_600" 56 /> 57 <CheckBox 58 android:id="@+id/class_checkbox3" 59 android:layout_width="wrap_content" 60 android:layout_height="wrap_content" 61 android:text="@string/class03" 62 android:layout_marginStart="20dp" 63 android:textColor="@color/grey_600" 64 /> 65 <CheckBox 66 android:id="@+id/class_checkbox4" 67 android:layout_width="wrap_content" 68 android:layout_height="wrap_content" 69 android:text="@string/class04" 70 android:layout_marginStart="20dp" 71 android:textColor="@color/grey_600" 72 /> 73 <CheckBox 74 android:id="@+id/class_checkbox5" 75 android:layout_width="wrap_content" 76 android:layout_height="wrap_content" 77 android:text="@string/class05" 78 android:layout_marginStart="20dp" 79 android:textColor="@color/grey_600" 80 /> 81 <CheckBox 82 android:id="@+id/class_checkbox6" 83 android:layout_width="wrap_content" 84 android:layout_height="wrap_content" 85 android:text="@string/class06" 86 android:layout_marginStart="20dp" 87 android:textColor="@color/grey_600" 88 /> 89 <CheckBox 90 android:id="@+id/class_checkbox7" 91 android:layout_width="wrap_content" 92 android:layout_height="wrap_content" 93 android:text="@string/class07" 94 android:layout_marginStart="20dp" 95 android:textColor="@color/grey_600" 96 /> 97 <CheckBox 98 android:id="@+id/class_checkbox8" 99 android:layout_width="wrap_content" 100 android:layout_height="wrap_content" 101 android:text="@string/class08" 102 android:layout_marginStart="20dp" 103 android:textColor="@color/grey_600" 104 /> 105 106 <LinearLayout 107 android:layout_width="match_parent" 108 android:layout_height="wrap_content" 109 android:orientation="horizontal"> 110 <Button 111 android:id="@+id/save_button" 112 android:layout_width="wrap_content" 113 android:layout_height="wrap_content" 114 android:layout_marginTop="32dp" 115 android:layout_marginBottom="20dp" 116 android:onClick="saveData" 117 android:text="@string/save_btn" 118 android:layout_gravity="center_horizontal" 119 android:layout_weight="1" 120 /> 121 122 </LinearLayout> 123 </LinearLayout> 124 </ScrollView> 125</androidx.constraintlayout.widget.ConstraintLayout>
kotlion
1package jp.co.test 2 3import android.annotation.SuppressLint 4import android.graphics.Color 5import android.os.Bundle 6import android.util.Log 7import android.view.LayoutInflater 8import android.view.View 9import android.view.ViewGroup 10import android.widget.Button 11import android.widget.CheckBox 12import android.widget.Toast 13import androidx.fragment.app.Fragment 14import kotlinx.coroutines.CoroutineScope 15import kotlinx.coroutines.Dispatchers 16import kotlinx.coroutines.launch 17import kotlinx.coroutines.withContext 18 19/** 20 * 設定 21 */ 22class SettingFragment : Fragment() { 23 24 // 保存ボタン 25 private lateinit var saveButton: Button 26 // 全てチェックボックスクラス 27 private lateinit var classAllCheckBox: CheckBox 28 29 // クラスチェックボックスMap 30 private var classCheckBoxMap = mutableMapOf<String, CheckBox>() 31 32 // 選択IDX 33 private var lstIdx:String? = "" 34 35 // クラスMap(key:名称、val:コード値) 36 private val classesMap = mapOf( 37 "0.15(3tクラス)以下" to "1", 38 "0.2" to "2", 39 "0.25" to "3", 40 "0.45" to "4", 41 "0.7" to "5", 42 "1.2" to "6", 43 "1.6以上" to "7", 44 "その他" to "8" 45 ) 46 /** 47 * onCreateView 48 * @return view 49 */ 50 override fun onCreateView( 51 inflater: LayoutInflater, container: ViewGroup?, 52 savedInstanceState: Bundle? 53 ): View? { 54 val view = inflater.inflate(R.layout.fragment_setting, container, false) 55 // putXXXXに対応するgetXXXXで値を取得 56 lstIdx = arguments?.getString("BUNDLE_KEY_IDX") // "プッシュ通知一覧選択IDX" 57 58 // リソースの初期化 59 initializeResource(view,lstIdx) 60 61 return view 62 } 63 64 /** 65 * リソースの初期化 66 * @param view 67 */ 68 private fun initializeResource(view: View, lstIdx: String?) { 69 Log.d(TAG, "initializeResource") 70 // 起動画像が表示されるため背景色を城に設定 71 view.setBackgroundColor(Color.WHITE) 72 // 保存ボタンの初期設定 73 saveButton = view.findViewById<Button>(R.id.save_button) 74 saveButton.setOnClickListener { 75 //saveData(attachmentConditions,false) 76 } 77 78 // 全てチェックの設定 79 // 全てチェックボックスクラス 80 classAllCheckBox = view.findViewById<CheckBox>(R.id.class_checkbox_all) 81 82 // 全てチェックボックスの設定 83 classAllCheckBox.setOnClickListener { 84 if(classAllCheckBox.isChecked){ 85 // チェックの場合、全て以外を非活性とする 86 for (map in classCheckBoxMap) { 87 val cb = map.value 88 cb.setEnabled(false) 89 //cb.setBackgroundColor(Color.GRAY) 90 } 91 }else{ 92 // チェック無しの場合、全て以外を活性とする 93 for (map in classCheckBoxMap) { 94 val cb = map.value 95 cb.setEnabled(true); 96 //cb.setBackgroundColor(Color.WHITE) 97 } 98 } 99 } 100 } 101 companion object { 102 const val TAG: String = "SettingFragment" 103 } 104} 105
試したこと
CheckBoxに以下のように背景色を設定すると
cb.setBackgroundColor(Color.GRAY)
ラベル全体がグレーになってしまいました。。
補足情報(FW/ツールのバージョンなど)
Android Studio Flamingo | 2022.2.1

回答2件
あなたの回答
tips
プレビュー