感谢您抽出时间来看这个问题。我正在尝试一些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的所有内容。有什么建议吗?
发布于 2012-05-11 05:30:55
首先,您希望在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;这可能不是最好的方法,但它应该足以让你开始实现你想要做的事情。
https://stackoverflow.com/questions/10542008
复制相似问题