我正在使用 MaterialButtonToggleGroup 创建选择器按钮。我想更改 MaterialButton 按钮的背景颜色。它的默认颜色是浅蓝色,我想将其更改为浅绿色。目前,我正在使用可绘制扇区来更改背景颜色,但它不起作用。
这是我的布局,
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:checkedButton="@id/btnOutline"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:visibility="visible"
app:layout_constraintRight_toRightOf="parent"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnOutline"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
android:text="@string/outline"
android:textAllCaps="false" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnMain"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:background="@drawable/button_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main"
android:textAllCaps="false" />
</com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud)
这是我的可绘制文件“button_selector”,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/selector_color"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
这是“selector_color”文件,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#93cdcb"/>
</shape>
Run Code Online (Sandbox Code Playgroud) 我想使用通用类型的 Moshi 适配器。
\n这是我的通用类型适配器代码,
\nfun <T> getObjectFromJson(typeOfObject: Class<T>, jsonString: String): T? {\n val moshi = Moshi.Builder().build()\n val jsonAdapter: JsonAdapter<T> = moshi.adapter<T>(\n typeOfObject::class.java\n )\n return jsonAdapter.fromJson(jsonString)!!\n}\nRun Code Online (Sandbox Code Playgroud)\n该代码不起作用。它抛出一个错误,
\n\n\n平台类 java.lang.Class 需要显式注册 JsonAdapter
\n
但是,如果我不\xe2\x80\x99t 使用这样的泛型类型,
\nfun getObjectFromJson(jsonString: String): UserProfile? {\n val moshi = Moshi.Builder().build()\n val jsonAdapter: JsonAdapter<UserProfile> = moshi.adapter<UserProfile>(\n UserProfile::class.java\n )\n return jsonAdapter.fromJson(jsonString)!!\n}\nRun Code Online (Sandbox Code Playgroud)\n然后代码就可以正常工作了。
\n这是UserProfile类,
\n @Parcelize\n@JsonClass(generateAdapter = true)\ndata class UserProfile(\n @get:Json(name = "p_contact")\n val pContact: String? = null,\n\n @get:Json(name = …Run Code Online (Sandbox Code Playgroud) 我在 Kotlin 中使用 MVVM 架构。我创建了一个 ViewModel 和 Repository 类。在 Repository 类中,我使用改造从 API 获取数据。我将该数据存储到我的 MutableLiveData 对象中,但是在调试对象时,即使我的 response.body 不为 null,该对象也始终显示为 null。
我对 Kotlin 很陌生。请帮忙..
ItemRepository.kt
class ItemRepository {
companion object{
private var instance: ItemRepository? = null
private var itemMutableList: MutableLiveData<List<ItemItem>>? = null
fun getinstance(): ItemRepository {
if (instance == null){
instance = ItemRepository()
}
return instance as ItemRepository
}
}
fun getItems() :MutableLiveData<List<ItemItem>>?{
var itemCall : Call<List<ItemItem>?> = ApiClient.getInstance()!!.getApi()!!.getIteams();
itemCall.enqueue(object : Callback<List<ItemItem>?> {
override fun onFailure(call: Call<List<ItemItem>?>, t: Throwable) {
}
override …Run Code Online (Sandbox Code Playgroud)