我有一个名为AnimatedView.java
的类,从ImageView
延伸,在此我有onTouchEvent
方法,其中我有一个int类型的值,命名为count.my要求我希望通过这个每次执行事件时都会为MainActivity赋值。请帮助我,这里我的代码是
主要活动是:
Context context;
int score=0;
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent();
int i=intent.getIntExtra("score",score);
EditText editText=(EditText) findViewById(R.id.editText1);
editText.setText(i);
}
public boolean onTouchEvent(MotionEvent event) {
Log.d("bharat", "ontouch called");
int touchType = event.getAction();
switch (touchType) {
case MotionEvent.ACTION_MOVE:
a = event.getX();
b = event.getY();
touching = true;
if (dm_touched) {
x = (int) a - bm_offsetx;
y = (int) b - bm_offsety;
}
break;
case MotionEvent.ACTION_DOWN:
//x and y give you your touch coordinates
a = event.getX();
b = event.getY();
touching = true;
Log.d("bharat", "action_down called");
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
bm_offsetx = (int) x - x;
bm_offsety = (int) y - y;
count++;
Log.i("bharat", "" + count);
Intent intent1 = new Intent();
intent1.putExtra("score", count);
}
dm_touched = true;
case MotionEvent.ACTION_UP:
default:
dm_touched = false;
touching = false;
}
return true;
}
答案 0 :(得分:1)
您创建一个名为TouchCountListener的接口,如下所示:
TouchCountListener.java
public interface TouchCountListener {
public void setTouchCount();
}
在活动中:
public class yourActivity extends Activity implements TouchCountListener {
int animatedViewTouchCount=0;
//..
@Override
protected void onCreate(Bundle savedInstanceState) {
View animatedView = findViewById(R.id.ID_OFYOURANIMATEDVIEW);
animatedView.setTouchCountListener(this);
///....
}
}
TouchCountListener mTouchCountListener;
int animatedViewTouchCount=0;
public void setTouchCountListener(TouchCountListener t){
mTouchCountListener=t;
}
public boolean onTouchEvent(MotionEvent event) {
mTouchCountListener.setTouchCount(++animatedViewTouchCount);
//...
}