[英]How to listen for state change in SwitchCompat widget?
如何監聽 SwitchCompat 小部件點擊? 我想在切換開關時執行一些語句。
尋找相當於
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do something
}
});
static Boolean isTouched = false;
switchButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
isTouched = true;
return false;
}
});
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isTouched) {
isTouched = false;
if (isChecked) {
}
else {
}
}
}
});
嘗試這個!
你只需要這個(setOnTouchListener 是不必要的):
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked) {
//if 'isChecked' is true do whatever you need...
}
else {
}
}
}
});
隨着 Butterknife SwitchCompat 狀態改變
@OnCheckedChanged(R.id.switchCompat)
public void onCheckedChanged(SwitchCompat switchCompat, boolean isChecked) {
Log.i("skh", "check:" + isChecked);
if (isChecked) {
// Log.i("skh","check:"+isChecked);
} else {
}
}
現在可以在同一個回調中使用 isPressed 方法進行檢查。
switchCompat.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView?.isPressed == true) {
// todo
} else {
}
}
我有同樣的問題 Slay 在對已接受答案的評論中解釋。 解決辦法來了!
我的解決方案,使用SwitchCompat
和 Kotlin。 在我的情況下,只有當用戶通過 UI 觸發更改時,我才需要對更改做出反應。 事實上,我的開關對LiveData
做出反應,這使得setOnClickListener
和setOnCheckedChangeListener
無法使用。 setOnClickListener
實際上對用戶交互做出正確反應,但如果用戶將拇指拖過開關,則不會觸發它。 如果以編程方式切換開關(例如由觀察者),另一端的setOnCheckedChangeListener
也會被觸發。 現在,在我的情況下,開關出現在兩個片段上,因此 onRestoreInstanceState 在某些情況下會觸發開關,舊值覆蓋正確的值。
所以,我查看了 SwitchCompat 的代碼,並且能夠成功地模仿它在區分點擊和拖動方面的行為,並使用它來構建一個可以正常工作的自定義觸摸偵聽器。 開始了:
/**
* This function calls the lambda function passed with the right value of isChecked
* when the switch is tapped with single click isChecked is relative to the current position so we pass !isChecked
* when the switch is dragged instead, the position of the thumb centre where the user leaves the
* thumb is compared to the middle of the switch, and we assume that left means false, right means true
* (there is no rtl or vertical switch management)
* The behaviour is extrapolated from the SwitchCompat source code
*/
class SwitchCompatTouchListener(private val v: SwitchCompat, private val lambda: (Boolean)->Unit) : View.OnTouchListener {
companion object {
private const val TOUCH_MODE_IDLE = 0
private const val TOUCH_MODE_DOWN = 1
private const val TOUCH_MODE_DRAGGING = 2
}
private val vc = ViewConfiguration.get(v.context)
private val mScaledTouchSlop = vc.scaledTouchSlop
private var mTouchMode = 0
private var mTouchX = 0f
private var mTouchY = 0f
/**
* @return true if (x, y) is within the target area of the switch thumb
* x,y and rect are in view coordinates, 0,0 is top left of the view
*/
private fun hitThumb(x: Float, y: Float): Boolean {
val rect = v.thumbDrawable.bounds
return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom
}
override fun onTouch(view: View, event: MotionEvent): Boolean {
if (view == v) {
when (MotionEventCompat.getActionMasked(event)) {
MotionEvent.ACTION_DOWN -> {
val x = event.x
val y = event.y
if (v.isEnabled && hitThumb(x, y)) {
mTouchMode = TOUCH_MODE_DOWN;
mTouchX = x;
mTouchY = y;
}
}
MotionEvent.ACTION_MOVE -> {
val x = event.x
val y = event.y
if (mTouchMode == TOUCH_MODE_DOWN &&
(abs(x - mTouchX) > mScaledTouchSlop || abs(y - mTouchY) > mScaledTouchSlop)
)
mTouchMode = TOUCH_MODE_DRAGGING;
}
MotionEvent.ACTION_UP,
MotionEvent.ACTION_CANCEL -> {
if (mTouchMode == TOUCH_MODE_DRAGGING) {
val r = v.thumbDrawable.bounds
if (r.left + r.right < v.width) lambda(false)
else lambda(true)
} else lambda(!v.isChecked)
mTouchMode = TOUCH_MODE_IDLE;
}
}
}
return v.onTouchEvent(event)
}
}
如何使用它:
實際的觸摸偵聽器,它接受帶有要執行的代碼的 lambda:
myswitch.setOnTouchListener(
SwitchCompatTouchListener(myswitch) {
// here goes all the code for your callback, in my case
// i called a service which, when successful, in turn would
// update my liveData
viewModel.sendCommandToMyService(it)
}
)
為了完整起見,這就是狀態switchstate
的觀察者(如果有的話)的樣子:
switchstate.observe(this, Observer {
myswitch.isChecked = it
})
其實你只需要這個:
if (yourSwitchButton.isChecked()) {
//do what you want here
} else{
//do what you want here
}
就這樣!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.