单击图像时我想要点击效果但是每当我第一次点击图像时我都没有发生任何事情,但是当我第二次点击它时会显示单击图像的橙色效果。下面是我的代码,我知道这可能不是正确的方法。但我想知道为什么会发生这种情况
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);
}
请帮助此代码显示此类行为的原因
答案 0 :(得分:1)
当您点击图像时,只需在图像上设置触控侦听器即可。 因此,而不是在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;
}
});
}
答案 1 :(得分:0)
尝试输入
<script>
function handleAccelerometer(event) {
var x = parseInt(event.accelerationIncludingGravity.x);
var y = parseInt(event.accelerationIncludingGravity.y);
var z = parseInt(event.accelerationIncludingGravity.z);
var acce = 'Acceleration:<br />';
acce += 'x: ' + x +'<br />y: ' + y + '<br />z: ' + z + '<br />';
accelerometer.innerHTML = acce;
}
function stopAccelerometer(event){
window.removeEventListener("devicemotion", handleAccelerometer);
}
function reloadAccelerometer(event){
location.reload();
}
function init() {
var accelerometer = document.getElementById('accelerometer');
var button = document.getElementById("stop");
if(window.DeviceMotionEvent) {
window.addEventListener('devicemotion', handleAccelerometer);
button.addEventListener("click", stopAccelerometer)
}
}
window.onload = init;
以上
startActivity(view.getId());
或
case MotionEvent.ACTION_CANCEL:
它对我有用。
希望这有帮助