MotionEvent 和触摸检测器?
MotionEvent and the touch detector?
我想请教一个问题:
我使用 MotionEvent 代码为我正在开发的问答游戏拖动了一个按钮,问题是我开始拖动后无法再点击被拖动的按钮,看起来像它始终保持在 ACTION_DOWN,我也尝试了 CANCEL 和 UP 操作,但还是一样!
另外,当拖动的按钮 [btn1] 触摸另一个按钮 [btn2] 时,如何让拖动的按钮在触摸另一个按钮或项目时执行某些操作(比如我希望它检测其他项目的触摸)例如,它会改变颜色!!
3 个问题:
起不来
点击不了了
触摸检测
有任何关于如何修复该问题的建议!提前联系
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.*;
import android.graphics.*;
import android.media.*;
import android.net.*;
import android.text.*;
import android.util.*;
import android.webkit.*;
import android.animation.*;
import android.view.animation.*;
import java.util.*;
import java.text.*;
import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;
import android.view.View;
public class MainActivity extends Activity {
private Timer _timer = new Timer();
private double res = 0;
private double lvl = 0;
private String message = "";
private LinearLayout linear3;
private TextView textview13;
private LinearLayout linear11;
private LinearLayout linear13;
private LinearLayout linear12;
private LinearLayout linear8;
private Button button1;
private TextView textview14;
private Button button4;
private Button button2;
private Button button3;
private TimerTask m;
@Override
protected void onCreate(Bundle _savedInstanceState) {
super.onCreate(_savedInstanceState);
setContentView(R.layout.main);
initialize();
initializeLogic();
}
private void initialize() {
linear3 = (LinearLayout) findViewById(R.id.linear3);
textview13 = (TextView) findViewById(R.id.textview13);
linear11 = (LinearLayout) findViewById(R.id.linear11);
linear13 = (LinearLayout) findViewById(R.id.linear13);
linear12 = (LinearLayout) findViewById(R.id.linear12);
linear8 = (LinearLayout) findViewById(R.id.linear8);
button1 = (Button) findViewById(R.id.button1);
textview14 = (TextView) findViewById(R.id.textview14);
button4 = (Button) findViewById(R.id.button4);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setOnTouchListener(new OnTouchListener() {
PointF DownPT = new PointF();
PointF StartPT = new PointF();
@Override public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
button1.setX((int)(StartPT.x+mv.x));
button1.setY((int)(StartPT.y+mv.y));
StartPT = new PointF(button1.getX(), button1.getY());
break;
case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(button1.getX(), button1.getY());
break;
case MotionEvent.ACTION_UP :
break;
default : break;
}
return true;
}
});
textview13.setText(message);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setBackgroundColor(0xFFCFD8DC);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
private void initializeLogic() {
/**
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
gd.setCornerRadius(30);**/
/* radius */
/*gd.setStroke(4, Color.WHITE);*/
/* stroke heigth and color */
m = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
lvl = 1;
textview14.setText("من هو مستر صلصا ؟");
button2.setText("مدير قناة صلصا");
button3.setText("صاحب سلسلة \nألف ليلة و ليلة");
button4.setText("لا أعلم");
}
});
}
};
_timer.schedule(m, (int)(1000));
m = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if ((((SketchwareUtil.getLocationX(button1) + 75) > (SketchwareUtil.getLocationX(button2) - 120)) && ((SketchwareUtil.getLocationX(button1) - 125) < (SketchwareUtil.getLocationX(button2) + 120))) && (((SketchwareUtil.getLocationY(button1) + 130) > (SketchwareUtil.getLocationY(button2) - 60)) && ((SketchwareUtil.getLocationY(button1) - 70) < (SketchwareUtil.getLocationY(button2) + 60)))) {
res = 1;
button2.setBackgroundColor(0xFF64DD17);
}
if ((((SketchwareUtil.getLocationX(button1) + 75) > (SketchwareUtil.getLocationX(button3) - 120)) && ((SketchwareUtil.getLocationX(button1) - 125) < (SketchwareUtil.getLocationX(button3) + 120))) && (((SketchwareUtil.getLocationY(button1) + 130) > (SketchwareUtil.getLocationY(button3) - 60)) && ((SketchwareUtil.getLocationY(button1) - 70) < (SketchwareUtil.getLocationY(button3) + 60)))) {
res = 2;
button3.setBackgroundColor(0xFFF44336);
}
if ((((SketchwareUtil.getLocationX(button1) + 75) > (SketchwareUtil.getLocationX(button4) - 120)) && ((SketchwareUtil.getLocationX(button1) - 125) < (SketchwareUtil.getLocationX(button4) + 120))) && (((SketchwareUtil.getLocationY(button1) + 130) > (SketchwareUtil.getLocationY(button4) - 60)) && ((SketchwareUtil.getLocationY(button1) - 70) < (SketchwareUtil.getLocationY(button4) + 60)))) {
res = 3;
button4.setBackgroundColor(0xFFF44336);
}
}
});
}
};
_timer.scheduleAtFixedRate(m, (int)(1000), (int)(100));
}
@Override
protected void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
super.onActivityResult(_requestCode, _resultCode, _data);
switch (_requestCode) {
default:
break;
}
}
改变这个
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setOnTouchListener(new OnTouchListener() {
PointF DownPT = new PointF();
PointF StartPT = new PointF();
@Override public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
button1.setX((int)(StartPT.x+mv.x));
button1.setY((int)(StartPT.y+mv.y));
StartPT = new PointF(button1.getX(), button1.getY());
break;
case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(button1.getX(), button1.getY());
break;
case MotionEvent.ACTION_UP :
break;
default : break;
}
return true;
}
});
textview13.setText(message);
}
});
到这个
int lastAction;
button1.setOnTouchListener(new OnTouchListener() {
PointF DownPT = new PointF();
PointF StartPT = new PointF();
@Override public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
button1.setX((int)(StartPT.x+mv.x));
button1.setY((int)(StartPT.y+mv.y));
StartPT = new PointF(button1.getX(), button1.getY());
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(button1.getX(), button1.getY());
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_UP :
if(lastAction == MotionEvent.ACTION_DOWN){
textview13.setText(message);
}
lastAction = MotionEvent.ACTION_UP;
break;
default : break;
}
return true;
}
});
对于第二部分,您需要获得其他项目的坐标,然后检查您的 event.getY()
或 event.getX()
是否在其他项目坐标的范围内。
哇!我无意中找到了解决方案
所有的问题!
我刚改了:
return true;
到 return false;
我只是想看看有什么不同!!
我想请教一个问题:
我使用 MotionEvent 代码为我正在开发的问答游戏拖动了一个按钮,问题是我开始拖动后无法再点击被拖动的按钮,看起来像它始终保持在 ACTION_DOWN,我也尝试了 CANCEL 和 UP 操作,但还是一样!
另外,当拖动的按钮 [btn1] 触摸另一个按钮 [btn2] 时,如何让拖动的按钮在触摸另一个按钮或项目时执行某些操作(比如我希望它检测其他项目的触摸)例如,它会改变颜色!!
3 个问题:
起不来
点击不了了
触摸检测
有任何关于如何修复该问题的建议!提前联系
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.*;
import android.graphics.*;
import android.media.*;
import android.net.*;
import android.text.*;
import android.util.*;
import android.webkit.*;
import android.animation.*;
import android.view.animation.*;
import java.util.*;
import java.text.*;
import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;
import android.view.View;
public class MainActivity extends Activity {
private Timer _timer = new Timer();
private double res = 0;
private double lvl = 0;
private String message = "";
private LinearLayout linear3;
private TextView textview13;
private LinearLayout linear11;
private LinearLayout linear13;
private LinearLayout linear12;
private LinearLayout linear8;
private Button button1;
private TextView textview14;
private Button button4;
private Button button2;
private Button button3;
private TimerTask m;
@Override
protected void onCreate(Bundle _savedInstanceState) {
super.onCreate(_savedInstanceState);
setContentView(R.layout.main);
initialize();
initializeLogic();
}
private void initialize() {
linear3 = (LinearLayout) findViewById(R.id.linear3);
textview13 = (TextView) findViewById(R.id.textview13);
linear11 = (LinearLayout) findViewById(R.id.linear11);
linear13 = (LinearLayout) findViewById(R.id.linear13);
linear12 = (LinearLayout) findViewById(R.id.linear12);
linear8 = (LinearLayout) findViewById(R.id.linear8);
button1 = (Button) findViewById(R.id.button1);
textview14 = (TextView) findViewById(R.id.textview14);
button4 = (Button) findViewById(R.id.button4);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setOnTouchListener(new OnTouchListener() {
PointF DownPT = new PointF();
PointF StartPT = new PointF();
@Override public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
button1.setX((int)(StartPT.x+mv.x));
button1.setY((int)(StartPT.y+mv.y));
StartPT = new PointF(button1.getX(), button1.getY());
break;
case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(button1.getX(), button1.getY());
break;
case MotionEvent.ACTION_UP :
break;
default : break;
}
return true;
}
});
textview13.setText(message);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setBackgroundColor(0xFFCFD8DC);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
private void initializeLogic() {
/**
android.graphics.drawable.GradientDrawable gd = new android.graphics.drawable.GradientDrawable();
gd.setCornerRadius(30);**/
/* radius */
/*gd.setStroke(4, Color.WHITE);*/
/* stroke heigth and color */
m = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
lvl = 1;
textview14.setText("من هو مستر صلصا ؟");
button2.setText("مدير قناة صلصا");
button3.setText("صاحب سلسلة \nألف ليلة و ليلة");
button4.setText("لا أعلم");
}
});
}
};
_timer.schedule(m, (int)(1000));
m = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if ((((SketchwareUtil.getLocationX(button1) + 75) > (SketchwareUtil.getLocationX(button2) - 120)) && ((SketchwareUtil.getLocationX(button1) - 125) < (SketchwareUtil.getLocationX(button2) + 120))) && (((SketchwareUtil.getLocationY(button1) + 130) > (SketchwareUtil.getLocationY(button2) - 60)) && ((SketchwareUtil.getLocationY(button1) - 70) < (SketchwareUtil.getLocationY(button2) + 60)))) {
res = 1;
button2.setBackgroundColor(0xFF64DD17);
}
if ((((SketchwareUtil.getLocationX(button1) + 75) > (SketchwareUtil.getLocationX(button3) - 120)) && ((SketchwareUtil.getLocationX(button1) - 125) < (SketchwareUtil.getLocationX(button3) + 120))) && (((SketchwareUtil.getLocationY(button1) + 130) > (SketchwareUtil.getLocationY(button3) - 60)) && ((SketchwareUtil.getLocationY(button1) - 70) < (SketchwareUtil.getLocationY(button3) + 60)))) {
res = 2;
button3.setBackgroundColor(0xFFF44336);
}
if ((((SketchwareUtil.getLocationX(button1) + 75) > (SketchwareUtil.getLocationX(button4) - 120)) && ((SketchwareUtil.getLocationX(button1) - 125) < (SketchwareUtil.getLocationX(button4) + 120))) && (((SketchwareUtil.getLocationY(button1) + 130) > (SketchwareUtil.getLocationY(button4) - 60)) && ((SketchwareUtil.getLocationY(button1) - 70) < (SketchwareUtil.getLocationY(button4) + 60)))) {
res = 3;
button4.setBackgroundColor(0xFFF44336);
}
}
});
}
};
_timer.scheduleAtFixedRate(m, (int)(1000), (int)(100));
}
@Override
protected void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
super.onActivityResult(_requestCode, _resultCode, _data);
switch (_requestCode) {
default:
break;
}
}
改变这个
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button1.setOnTouchListener(new OnTouchListener() {
PointF DownPT = new PointF();
PointF StartPT = new PointF();
@Override public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
button1.setX((int)(StartPT.x+mv.x));
button1.setY((int)(StartPT.y+mv.y));
StartPT = new PointF(button1.getX(), button1.getY());
break;
case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(button1.getX(), button1.getY());
break;
case MotionEvent.ACTION_UP :
break;
default : break;
}
return true;
}
});
textview13.setText(message);
}
});
到这个
int lastAction;
button1.setOnTouchListener(new OnTouchListener() {
PointF DownPT = new PointF();
PointF StartPT = new PointF();
@Override public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
button1.setX((int)(StartPT.x+mv.x));
button1.setY((int)(StartPT.y+mv.y));
StartPT = new PointF(button1.getX(), button1.getY());
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_DOWN : DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(button1.getX(), button1.getY());
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_UP :
if(lastAction == MotionEvent.ACTION_DOWN){
textview13.setText(message);
}
lastAction = MotionEvent.ACTION_UP;
break;
default : break;
}
return true;
}
});
对于第二部分,您需要获得其他项目的坐标,然后检查您的 event.getY()
或 event.getX()
是否在其他项目坐标的范围内。
哇!我无意中找到了解决方案
所有的问题!
我刚改了:
return true;
到 return false;
我只是想看看有什么不同!!