Java 我的onTouch方法并不是按照编程来做的
我正在制作一个Android应用程序。这个应用程序的目标是让人们在触摸时看到一些东西。更详细地说,这个应用程序将显示两种类型的圆圈,一种是红色的,另一种是绿色的。圆的颜色存储在lastColor变量中。颜色的用途是,当用户“触摸”红色或绿色圆圈时,会发生一些事情。例如,如果触摸到一个绿色圆圈,一个点被添加到该分数中,或者如果单击了一个红色圆圈,则活动会发生更改,但当单击其中一个圆圈时,什么也不会发生。这是onTouch方法Java 我的onTouch方法并不是按照编程来做的,java,android,ontouchlistener,touch-event,ontouch,Java,Android,Ontouchlistener,Touch Event,Ontouch,我正在制作一个Android应用程序。这个应用程序的目标是让人们在触摸时看到一些东西。更详细地说,这个应用程序将显示两种类型的圆圈,一种是红色的,另一种是绿色的。圆的颜色存储在lastColor变量中。颜色的用途是,当用户“触摸”红色或绿色圆圈时,会发生一些事情。例如,如果触摸到一个绿色圆圈,一个点被添加到该分数中,或者如果单击了一个红色圆圈,则活动会发生更改,但当单击其中一个圆圈时,什么也不会发生。这是onTouch方法 public boolean onTouch(View v, Motio
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y) == true){
//Do your things here
if(redColor == lastColor){
Intent i = new Intent(v.getContext(), YouFailed.class);
v.getContext().startActivity(i);
} else {
addPoints++;
}
}else {
}
return true;
}
代码的这一部分功能不正常。下面是全班同学
public class DrawingView extends View {
public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RectF rectf = new RectF(0, 0, 200, 0);
private static final int w = 100;
public static int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
public static int redColor = Color.RED;
public static int greenColor = Color.GREEN;
int randomWidth = 0;
int randomHeight = 0;
public static int addPoints = 0;
private final Runnable updateCircle = new Runnable() {
@Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
if (random == null) {
randomWidth = (int) (random.nextInt(Math.abs(getWidth() - radius / 2)) + radius / 2f);
randomHeight = (random.nextInt((int) Math.abs((getHeight() - radius / 2 + radius / 2f))));
}
else {
randomWidth = (int) (random.nextInt(Math.abs(getWidth() - radius / 2)) + radius / 2f);
randomHeight = (random.nextInt((int) Math.abs((getHeight() - radius / 2 + radius / 2f))));
}
canvas.drawCircle(randomWidth, randomHeight + radius / 2f, radius, paint);
paint.setColor(Color.BLACK);
paint.setTextSize(150);
canvas.drawText("Score: " + addPoints, 120, 300, paint);
}
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if (isInsideCircle(x, y) == true) {
//Do your things here
if (redColor == lastColor) {
Intent i = new Intent(v.getContext(), YouFailed.class);
v.getContext().startActivity(i);
}
else {
addPoints++;
}
}
else {
}
return true;
}
public boolean isInsideCircle(int x, int y) {
if ((((x - randomWidth) * (x - randomWidth)) + ((y - randomHeight) * (y - randomHeight))) < ((radius) * (radius)))
return true;
return false;
}
}
公共类DrawingView扩展视图{
公共绘图视图(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
}
RectF RectF=新的RectF(0,0,200,0);
专用静态最终int w=100;
公共静态int lastColor=Color.BLACK;
私有最终随机=新随机();
私人最终油漆=新油漆();
专用最终整数半径=230;
私有最终处理程序=新处理程序();
公共静态int redColor=Color.RED;
公共静态int greenColor=Color.GREEN;
int-randomWidth=0;
int-randomHeight=0;
公共静态int addPoints=0;
private final Runnable updateCircle=new Runnable(){
@凌驾
公开募捐{
lastColor=random.nextInt(2)==1?红色:绿色;
paint.setColor(lastColor);
使无效();
handler.postDelayed(这个,1000);
}
};
@凌驾
受保护的无效数据附加到DOWINDOW(){
super.onAttachedToWindow();
handler.post(更新循环);
}
@凌驾
受保护的无效onDetachedFromWindow(){
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
//你的其他东西在这里
if(random==null){
randomWidth=(int)(random.nextInt(Math.abs(getWidth()-radius/2))+radius/2f);
randomHeight=(random.nextInt((int)Math.abs((getHeight()-radius/2+radius/2f));
}
否则{
randomWidth=(int)(random.nextInt(Math.abs(getWidth()-radius/2))+radius/2f);
randomHeight=(random.nextInt((int)Math.abs((getHeight()-radius/2+radius/2f));
}
画布.画圈(随机宽度,随机高度+半径/2f,半径,绘制);
油漆。设置颜色(颜色。黑色);
油漆.尺寸(150);
canvas.drawText(“分数:+addPoints,120300,paint”);
}
公共布尔onTouch(视图v,运动事件){
int x=(int)event.getX();
int y=(int)event.getY();
如果(isInsideCircle(x,y)=真){
//在这里做你的事情
if(redColor==lastColor){
Intent i=新Intent(v.getContext(),YouFailed.class);
v、 getContext().startActivity(i);
}
否则{
addPoints++;
}
}
否则{
}
返回true;
}
公共布尔IsInsidercle(整数x,整数y){
如果(((x-随机宽度)*(x-随机宽度))+((y-随机高度)*(y-随机高度))<((半径)*(半径)))
返回true;
返回false;
}
}
您需要覆盖onTouchEvent功能。我测试了代码,它完全可以工作!另外,确保addPoints++之后返回false,以确保只添加一个点。希望这对你有用
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if (isInsideCircle(x, y)) {
//Do your things here
if (redColor == lastColor) {
Intent i = new Intent(getContext(), null);
getContext().startActivity(i);
}
else {
addPoints++;
return false;
}
}
else {
}
return true;
}
“当被触碰时会发生什么事”比如什么?“这部分代码是不能正常工作的”怎么说?当一个绿色或红色的圆圈被“触摸”时,它应该做些什么。如果它是一个红色的圆圈,它将显示另一个活动如果它是一个绿色的圆圈,它将添加一个点,但当我点击任何地方时,当然不会发生任何事情,它是崩溃还是什么都不做?您是否放置了日志和/或断点以查看调用了什么/没有调用什么?目前,这是一个相当广泛的问题,缺乏自我调试,这使得其他人很难解决。当我“触摸”或单击模拟器日志或断点时,没有崩溃,只是什么都不做?日志是否与logcat中的日志相同?logcat中没有错误