我正在Android Studio上创建一个简单的应用程序(游戏),我的问题是: 如果语句在触摸事件中似乎无法正常工作,则为两个单独的视图添加了两个触摸事件。 这是我的代码,如果您能提供帮助,我将非常高兴!
这是MainActivity.java代码:
df['year2']=df['Datetime'].dt.strftime('%y')
print(df)
c0 c1 c2 Datetime year year2
20000701 1 1 1 2000-07-01 2000 00
20010701 2 2 2 2001-07-01 2001 01
这是我的activity_main.xml代码:
package com.example.splitapp;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private RelativeLayout rel1 = null;
private RelativeLayout rel2 = null;
private int countA = 0;
private int countB = 0;
private boolean end = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rel1 = (RelativeLayout) findViewById(R.id.rel1);
rel2 = (RelativeLayout) findViewById(R.id.rel2);
rel1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(!end) {
countA = countA+1;
countB = countB-1;
if(countA == 10 && countB < 10){
rel1.setBackgroundColor(Color.parseColor("#00FF00"));
rel2.setBackgroundColor(Color.parseColor("#FF0000"));
end = true;
}
} else {
countA = 0;
countB = 0;
rel1.setBackgroundColor(Color.parseColor("#BA55D3"));
rel2.setBackgroundColor(Color.parseColor("#8A2BE2"));
end = false;
}
return true;
}
});
rel2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(!end) {
countB++;
countA--;
if(countB == 10 && countA < 10) {
rel1.setBackgroundColor(Color.parseColor("#FF0000"));
rel2.setBackgroundColor(Color.parseColor("#00FF00"));
end = true;
}
} else {
countA = 0;
countB = 0;
rel1.setBackgroundColor(Color.parseColor("#BA55D3"));
rel2.setBackgroundColor(Color.parseColor("#8A2BE2"));
end = false;
}
return true;
}
});
}
}
答案 0 :(得分:1)
理论:您的评论中描述的问题基于以下事实:onTouch方法是在每个触摸事件(包括每个UP
,{{1} },DOWN
等事件,如docs中所述)。请参阅https://developer.android.com/reference/android/view/View.OnTouchListener.html和https://developer.android.com/reference/android/view/MotionEvent.html了解更多信息。
解决方案:如果您只想对MOVE
上的轻按做出反应,只需为其添加OnClickListener:
RelativeLayout