Android 双击需要通过onClick()在imageButton上显示单击效果
我想要点击图像时的点击效果,但是从我的代码来看,每当我第一次点击图像时,什么都不会发生。但是,当我第二次点击时,它会显示图像被点击时的橙色效果。下面是我的代码,我知道这可能不是正确的方法。但我想知道为什么会这样 image.xmlAndroid 双击需要通过onClick()在imageButton上显示单击效果,android,ontouchlistener,android-imagebutton,Android,Ontouchlistener,Android Imagebutton,我想要点击图像时的点击效果,但是从我的代码来看,每当我第一次点击图像时,什么都不会发生。但是,当我第二次点击时,它会显示图像被点击时的橙色效果。下面是我的代码,我知道这可能不是正确的方法。但我想知道为什么会这样 image.xml <ImageView style="@style/icon" android:background="@drawable/fear_96" android:onCli
<ImageView
style="@style/icon"
android:background="@drawable/fear_96"
android:onClick="see"
android:id="@+id/abulation"
/>
请帮助解释此代码显示此类行为的原因请尝试将
startActivity(view.getId());
高于
case MotionEvent.ACTION_CANCEL:
或
这对我有用。
希望这对你有帮助,当你第一次点击图像时,它只是在图像上设置了触摸监听器。 因此,不要在see函数中向图像添加触控监听器。 用这个试试
<ImageView
style="@style/icon"
android:background="@drawable/fear_96"
android:id="@+id/abulation" />
ImageView imageView = (ImageView)findViewById(R.id.abulation);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
view.getBackground().setColorFilter(0xf0f47521, PorterDuff.Mode.SRC_ATOP);
view.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
view.getBackground().clearColorFilter();
view.invalidate();
startActivity(view.getId());
break;
}
return true;
}
});
}
ImageView ImageView=(ImageView)findViewById(R.id.abulation);
imageView.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图、运动事件、运动事件){
开关(motionEvent.getAction()){
case MotionEvent.ACTION\u DOWN:
view.getBackground().setColorFilter(0xf0f47521,PorterDuff.Mode.SRC_);
view.invalidate();
打破
case MotionEvent.ACTION\u取消:
case MotionEvent.ACTION\u UP:
view.getBackground().clearColorFilter();
view.invalidate();
startActivity(view.getId());
打破
}
返回true;
}
});
}
只需使用AlphaAnimation或ObjectAnimator我的问题是为什么它会显示这种行为?我知道还有其他方法可以做到这一点。请先阅读我问题的最后一行,然后再简单地往下看。我有大约45个imageButton,我不想做那么多东西,所以我只是想用这种方式来做。我也这么想,但很高兴知道这背后的真正原因。无论如何,谢谢你的回答。
case MotionEvent.ACTION_UP:
<ImageView
style="@style/icon"
android:background="@drawable/fear_96"
android:id="@+id/abulation" />
ImageView imageView = (ImageView)findViewById(R.id.abulation);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
view.getBackground().setColorFilter(0xf0f47521, PorterDuff.Mode.SRC_ATOP);
view.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
view.getBackground().clearColorFilter();
view.invalidate();
startActivity(view.getId());
break;
}
return true;
}
});
}