实现 OnClickListener?

发布于 2024-10-01 05:49:02 字数 3637 浏览 2 评论 0原文

我正在尝试做一些类似于 Android 锁屏模式的事情。我有一个类扩展了我创建的多个实例的视图。这些同时出现在屏幕上。

我需要能够单独单击它们并使每个单独变绿,但是只有一个触摸监听器同时监听并且它属于最后出现的点,因此如果我单击屏幕上的任意位置,最后出现的点无论我点击哪里都会变成绿色。

这是我的点类的代码:

package com.ewebapps;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class Dot extends View implements OnTouchListener {
     private final float x;
     private final float y;
     private final int r;
     private final Paint mBlack = new Paint(Paint.ANTI_ALIAS_FLAG);
     private final Paint mWhite = new Paint(Paint.ANTI_ALIAS_FLAG);
     private final Paint mGreen = new Paint(Paint.ANTI_ALIAS_FLAG);
     private boolean touched;
/*     
     public boolean onInterceptTouchEvent(View v, MotionEvent event) {
   touched = true;
      //mPaint.setColor(0xFF00FF00); // Turn dot green.
      invalidate();
   return true;
  }
*/
     public Dot(Context context, float x, float y, int r) {
         super(context);
         mBlack.setColor(0xFF000000); //Black
         mWhite.setColor(0xFFFFFFFF); //White
         mGreen.setColor(0xFF00FF00); //Green
         this.x = x;
         this.y = y;
         this.r = r;
         this.setOnTouchListener(new OnTouchListener(){

                @Override

                public boolean onTouch(View v, MotionEvent event) {

                 touched = true;
                 //mPaint.setColor(0xFF00FF00); // Turn dot green.
              v.invalidate();  
              return true;

                }
        });

     }

 /*    @Override
  public boolean dispatchTouchEvent(MotionEvent event) { // On touch.
      touched = true;
      //mPaint.setColor(0xFF00FF00); // Turn dot green.
      this.invalidate();
         return super.dispatchTouchEvent(event);
     }
  */
     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
         canvas.drawCircle(x, y, r+2, mWhite); //White stroke.

         if(!touched)
         {
          canvas.drawCircle(x, y, r, mBlack); //Black circle.
         }
         else
         {
          canvas.drawCircle(x, y, r, mGreen); //Green circle.
         }
     }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
  return false;
 }



/* @Override
 public boolean onTouch(View v, MotionEvent event) {
   touched = true;
     //mPaint.setColor(0xFF00FF00); // Turn dot green.
     invalidate();
     return true;
 }
*/
}

这是我的主类:

package com.ewebapps;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
import android.widget.FrameLayout;


public class dots extends Activity{

 public Boolean isRunning, isPaused;
 public int maxdots = 20;
 public int currentdotdraw = 1;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


    public void newdotdraw(){

    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int screenwidth = display.getWidth();
    int screenheight = display.getHeight();

    int x = (int) (Math.random() * screenwidth) + 1; 
    int y = (int) (Math.random() * screenheight) + 1;  

    FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
    main.addView(new Dot(this,x,y,25));

    }

}

在代码中我多次调用 newdotdraw 。

I am trying to do something similar to that of the android lock screen pattern. I have a class that extends a view that I create multiple instances of. These appear on the screen all at once.

I need to be able to click on them individually and have each one turn green individually, however only one on touch listener is listening at once and it belongs to the last dot which appeared, so if I click anywhere on the screen the last appeared dot turns green no matter where I click.

Here is the code for my dot class:

package com.ewebapps;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class Dot extends View implements OnTouchListener {
     private final float x;
     private final float y;
     private final int r;
     private final Paint mBlack = new Paint(Paint.ANTI_ALIAS_FLAG);
     private final Paint mWhite = new Paint(Paint.ANTI_ALIAS_FLAG);
     private final Paint mGreen = new Paint(Paint.ANTI_ALIAS_FLAG);
     private boolean touched;
/*     
     public boolean onInterceptTouchEvent(View v, MotionEvent event) {
   touched = true;
      //mPaint.setColor(0xFF00FF00); // Turn dot green.
      invalidate();
   return true;
  }
*/
     public Dot(Context context, float x, float y, int r) {
         super(context);
         mBlack.setColor(0xFF000000); //Black
         mWhite.setColor(0xFFFFFFFF); //White
         mGreen.setColor(0xFF00FF00); //Green
         this.x = x;
         this.y = y;
         this.r = r;
         this.setOnTouchListener(new OnTouchListener(){

                @Override

                public boolean onTouch(View v, MotionEvent event) {

                 touched = true;
                 //mPaint.setColor(0xFF00FF00); // Turn dot green.
              v.invalidate();  
              return true;

                }
        });

     }

 /*    @Override
  public boolean dispatchTouchEvent(MotionEvent event) { // On touch.
      touched = true;
      //mPaint.setColor(0xFF00FF00); // Turn dot green.
      this.invalidate();
         return super.dispatchTouchEvent(event);
     }
  */
     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
         canvas.drawCircle(x, y, r+2, mWhite); //White stroke.

         if(!touched)
         {
          canvas.drawCircle(x, y, r, mBlack); //Black circle.
         }
         else
         {
          canvas.drawCircle(x, y, r, mGreen); //Green circle.
         }
     }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
  return false;
 }



/* @Override
 public boolean onTouch(View v, MotionEvent event) {
   touched = true;
     //mPaint.setColor(0xFF00FF00); // Turn dot green.
     invalidate();
     return true;
 }
*/
}

And here is my main class:

package com.ewebapps;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
import android.widget.FrameLayout;


public class dots extends Activity{

 public Boolean isRunning, isPaused;
 public int maxdots = 20;
 public int currentdotdraw = 1;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


    public void newdotdraw(){

    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int screenwidth = display.getWidth();
    int screenheight = display.getHeight();

    int x = (int) (Math.random() * screenwidth) + 1; 
    int y = (int) (Math.random() * screenheight) + 1;  

    FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
    main.addView(new Dot(this,x,y,25));

    }

}

In the code I called newdotdraw multiple times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2024-10-08 05:49:02

您可以在顶部(覆盖)和顶部添加透明的自定义视图吗?然后跟踪触摸事件并查看它们属于哪个按钮。 (简单的数学)。

这也将使您更容易绘制线条(类似于解锁图案)。

Could you add a transparent custom view on top (overlay) & then track the touch events and see to which button they belong. (simple math).

That would make it easier for you to draw lines (similar to the unlock pattern), too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文