Thanx花时间看问题。我正在尝试一些Android编程,我已经碰壁了;不知道如何解决它。我正在尝试仅在触摸计数为奇数时激活特定实体的动画。那是touchCount%2 != 0
。
public boolean onTouch(View v, MotionEvent event){
ArrayList<TextView> textToDance = new ArrayList<TextView>();
textToDance.add((TextView)findViewById(R.id.CAD5));
textToDance.add((TextView)findViewById(R.id.CAD10));
textToDance.add((TextView)findViewById(R.id.CAD20));
textToDance.add((TextView)findViewById(R.id.CAD50));
textToDance.add((TextView)findViewById(R.id.CAD100));
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
for(TextView txtAnimate: textToDance){
if(event.getRawX()<= txtAnimate.getX()+txtAnimate.getMeasuredWidth() && event.getRawX()>=txtAnimate.getX()){
if(event.getRawY()<= txtAnimate.getY()+105+txtAnimate.getMeasuredHeight() && event.getRawY()>=txtAnimate.getY()+105){
helpAnimate(txtAnimate, 0);
}
}
}
break;
case MotionEvent.ACTION_MOVE:
Log.d("MOVE","MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d("UP","UP");
break;
default:
break;
}
return true;
}
我尝试实现一个HashMap,但该地图重置了所有onTouch被调用。有什么建议吗?
答案 0 :(得分:0)
首先,您希望在onTouch
方法之外初始化数组。
ArrayList<TextView> textToDance = new ArrayList<TextView>();
textToDance.add((TextView)findViewById(R.id.CAD5));
textToDance.add((TextView)findViewById(R.id.CAD10));
textToDance.add((TextView)findViewById(R.id.CAD20));
textToDance.add((TextView)findViewById(R.id.CAD50));
textToDance.add((TextView)findViewById(R.id.CAD100));
然后你可以创建一个地图并用零计数值初始化它并注册一个onTouchListener(让我们假设它是你的Activity
)
HashMap<TextView,Integer> myMap = new HashMap<TextView,Integer>();
for (TextView tv : textToDance){
tv.setOnTouchListener(this);
myMap.put(tv,0);
}
然后在onTouch
中你会有类似......
public boolean onTouch(View v, MotionEvent event){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if (myMap.contains(v)){
myMap.put(v, myMap.get(v) + 1;
}
break;
这可能不是最理想的方式,但它应该足以让你开始实现你想要做的事情。