使用 jetpack compose,对于点击事件如何执行触觉反馈。我是jetpack compose的新手。这就是我尝试过的 -
val hapticFeedback = LocalHapticFeedback
@Composable
fun Tab() {
Row() {
Icon(imageVector = icon, contentDescription = text)
if (selected) {
// i tried both the following ways, none are working.
hapticFeedback.current.performHapticFeedback(
HapticFeedbackType(10)
)
hapticFeedback.current.performHapticFeedback(HapticFeedbackType.TextHandleMove)
....
Spacer(Modifier.width(12.dp))
Text(text.uppercase(Locale.getDefault()))
}
}
}
Run Code Online (Sandbox Code Playgroud)
当文本被选中时,我可以看到文本,但没有得到微妙的振动反馈。
我正在尝试从Kotlin中的后台线程更新回收者视图的内容。我没有使用AsyncTask。
这是我的代码,我想知道是否有比这更好的方法:
在我的MainActivity中,我将progressThread作为成员变量。
var progressThread = Thread()
Run Code Online (Sandbox Code Playgroud)
然后在我想首先运行线程的方法中,我正在定义它...就像
progressThread = Thread (
Runnable {
kotlin.run {
try {
while (i <= 100 && !progressThread.isInterrupted) {
Thread.sleep(200)
//Some Logic
runOnUiThread {
//this runs in ui thread
}
i++
}
}catch (e:InterruptedException){
progressThread.interrupt()
}
}
})
Run Code Online (Sandbox Code Playgroud)
之后,我将以与
progressThread.start()
Run Code Online (Sandbox Code Playgroud)
为了停止它,我有一个监听器来取消进度,并且在该监听器的回调中,我写了:
progressThread.interrupt()
Run Code Online (Sandbox Code Playgroud) 这是我的简单选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_square_blue_border_unselected" android:state_checked="false" />
<item android:drawable="@drawable/ic_square_checkbox_selected" android:state_checked="true" />
<item android:drawable="@drawable/ic_square_blue_border_unselected" />
</selector>
Run Code Online (Sandbox Code Playgroud)
当我添加state_enabled : false/true 时,它说,
“此项目无法访问,因为前一个项目是更一般的匹配”
……我不明白为什么会这样?因为我还添加了一个新属性 -> state_enabled ...我想如果两个属性都匹配然后只选择相应的可绘制对象。
我想做类似的事情,
checked && not disabled -> blue color checked box,
not checked && not disabled -> blue border unchecked box,
checked && disabled -> gray color checked box,
not checked && disabled -> gray color unchecked box
Run Code Online (Sandbox Code Playgroud)
而且, checkbox.setEnabled(false) 不适用于这种情况。这是我的 xml 代码。
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/selector_chkbox_blue_border"
android:textColor="@color/steel_grey"
android:paddingLeft="10dp"
android:text="Checkbox text"/> …Run Code Online (Sandbox Code Playgroud)