ImageView 點擊中換圖, 放開恢復原圖 === 可以多加一個監聽事件,只要點擊就會觸發,不必等到偵測長按事件才更新圖片: ```java= // 必須複寫此方法, 在 onTouch 的 MotionEvent.ACTION_UP 才會觸發 imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { logger.info("onClick"); } }); imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { int action = motionEvent.getAction(); switch (action) { // 點擊時換成按下的圖, 長按不放效果也一樣 case MotionEvent.ACTION_DOWN: iv_pttBtn.setImageResource(R.drawable.pressed); logger.info("action: ACTION_DOWN"); break; // 放開換成原本的圖 case MotionEvent.ACTION_UP: iv_pttBtn.setImageResource(R.drawable.btn_ptt_normal); logger.info("action: ACTION_UP"); break; } return false; } }); ``` ## Ref. [Android 实现ImageView长按显示新图片,松开恢复原图](https://blog.csdn.net/weixin_42664622/article/details/108286104) [【Touch】触摸事件onTouch](https://blog.csdn.net/u012391876/article/details/54408804) ###### tags: `實作相關`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up