imageButton通过onClick()双击显示点击效果
It takes double click to show click effect on imageButton through onClick()
我想要点击图片时的点击效果,但是从我的代码来看,每当我第一次点击图片时,什么都没有发生,但是当我第二次点击时,它显示橙色效果,即图片被点击。下面是我的代码,我知道这可能不是正确的方法。但我想知道为什么会这样
image.xml
<ImageView
style="@style/icon"
android:background="@drawable/fear_96"
android:onClick="see"
android:id="@+id/abulation"
/>
下面是onclick方法
public void see(View view) {
view.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;
}
});
}
public void startActivity(int id)
{
Intent intent=new Intent(this,DuwaListView.class);
switch (id)
{
case R.id.abulation:
intent.putExtra(Intent.EXTRA_TEXT,"Abulation");
break;
case R.id.dressing:
intent.putExtra(Intent.EXTRA_TEXT,"Dressing");
break;
case R.id.restroom:
intent.putExtra(Intent.EXTRA_TEXT,"Restroom");
break;
default:
Toast.makeText(this,"underconstruction",Toast.LENGTH_SHORT).show();
return;
}
startActivity(intent);
}
请帮忙解释为什么这段代码会出现这种行为
尝试将
startActivity(view.getId());
在
以上
case MotionEvent.ACTION_CANCEL:
或
case MotionEvent.ACTION_UP:
它对我有用。
希望这有帮助
当您第一次点击图像时,它只是在图像上设置 Touch Listener。
因此,而不是在 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;
}
});
}
我想要点击图片时的点击效果,但是从我的代码来看,每当我第一次点击图片时,什么都没有发生,但是当我第二次点击时,它显示橙色效果,即图片被点击。下面是我的代码,我知道这可能不是正确的方法。但我想知道为什么会这样
image.xml
<ImageView
style="@style/icon"
android:background="@drawable/fear_96"
android:onClick="see"
android:id="@+id/abulation"
/>
下面是onclick方法
public void see(View view) {
view.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;
}
});
}
public void startActivity(int id)
{
Intent intent=new Intent(this,DuwaListView.class);
switch (id)
{
case R.id.abulation:
intent.putExtra(Intent.EXTRA_TEXT,"Abulation");
break;
case R.id.dressing:
intent.putExtra(Intent.EXTRA_TEXT,"Dressing");
break;
case R.id.restroom:
intent.putExtra(Intent.EXTRA_TEXT,"Restroom");
break;
default:
Toast.makeText(this,"underconstruction",Toast.LENGTH_SHORT).show();
return;
}
startActivity(intent);
}
请帮忙解释为什么这段代码会出现这种行为
尝试将
startActivity(view.getId());
在
以上case MotionEvent.ACTION_CANCEL:
或
case MotionEvent.ACTION_UP:
它对我有用。 希望这有帮助
当您第一次点击图像时,它只是在图像上设置 Touch Listener。 因此,而不是在 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;
}
});
}