【发布时间】:2018-06-24 04:44:11
【问题描述】:
我想问一个问题:
我使用 MotionEvent 代码拖动我正在开发的问答游戏的按钮,问题是在我开始拖动它后我无法再单击拖动的按钮,它看起来像它停留总是在 ACTION_DOWN,我也尝试了 CANCEL 和 UP 动作,但还是一样!
此外,当拖动的按钮 [btn1] 触摸另一个按钮 [btn2] 时,我如何让拖动的按钮在触摸另一个按钮或项目时执行某些操作(就像我希望它检测到其他项目的触摸一样)例如改变颜色!!
3 个问题:
起不来
你不能再点击它了
触摸检测
任何建议如何解决这个问题!并提前 ty
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;
}
}
【问题讨论】:
-
您可能想粘贴一个代码示例,并在您尝试过的问题中更详细地介绍一下。
-
哦,好的,我会放代码
-
对。您还需要使用正确的大写字母和标点符号。我们不是lolcats。我应该是我,你应该是你。逗号或感叹号前也不需要空格。
-
好的,对不起,我是新来的,请教你的建议
-
你为你的按钮设置了 onClickListener 吗?
标签: java android object-detection motionevent