OntouchListener向数组添加太多值

时间:2015-03-11 20:27:41

标签: java android arraylist onclicklistener ontouchlistener

对于我正在处理的应用程序,我需要使用OntouchListeners,以便我的按钮的透明区域不可点击,但这是一个我没有预料到的问题。当我按下按钮时,每次都会存储多个值。

这是我的代码:

private final OnTouchListener middle = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
            int color = bmp.getPixel((int) event.getX(), (int) event.getY());
            if (color == Color.TRANSPARENT)
                return false;
            else {


                Random rand = new Random();
                int rndInt = rand.nextInt(5)+ 1 ;   
                list.add(rndInt);
                 Log.d("VALUE", Integer.toString(rndInt));
                 Log.i("VALUE LIST ", list.toString()+" <<<<LIST HERE"); 
                return true;
            }
        }
    };

以下是显示问题的日志。从这里我只按了3次按钮,所以只存储3个值:

       03-11 20:23:53.188: D/LOOK(1386): 2
03-11 20:23:53.188: I/VALUE LIST(1386): [2] <<<<LIST HERE
03-11 20:23:53.218: D/-heap(1386): GC_FOR_ALLOC freed 268K, 56% free 8369K/18851K, paused 19ms
03-11 20:23:53.228: D/LOOK(1386): 5
03-11 20:23:53.228: I/VALUE LIST(1386): [2, 5] <<<<LIST HERE
03-11 20:23:53.238: D/LOOK(1386): 5
03-11 20:23:53.238: I/VALUE LIST(1386): [2, 5, 5] <<<<LIST HERE
03-11 20:23:58.698: D/-heap(1386): GC_FOR_ALLOC freed 1849K, 58% free 7942K/18851K, paused 32ms
03-11 20:23:58.698: D/LOOK(1386): 3
03-11 20:23:58.698: I/VALUE LIST(1386): [2, 5, 5, 3] <<<<LIST HERE
03-11 20:23:58.718: D/LOOK(1386): 4
03-11 20:23:58.718: I/VALUE LIST(1386): [2, 5, 5, 3, 4] <<<<LIST HERE
03-11 20:23:58.728: D/LOOK(1386): 3
03-11 20:23:58.738: I/VALUE LIST(1386): [2, 5, 5, 3, 4, 3] <<<<LIST HERE
03-11 20:24:00.218: D/-heap(1386): GC_FOR_ALLOC freed 1708K, 58% free 7942K/18851K, paused 33ms
03-11 20:24:00.228: D/LOOK(1386): 5
03-11 20:24:00.228: I/VALUE LIST(1386): [2, 5, 5, 3, 4, 3, 5] <<<<LIST HERE
03-11 20:24:00.238: D/LOOK(1386): 5
03-11 20:24:00.238: I/VALUE LIST(1386): [2, 5, 5, 3, 4, 3, 5, 5] <<<<LIST HERE
03-11 20:24:00.268: D/-heap(1386): GC_FOR_ALLOC freed 712K, 55% free 8511K/18851K, paused 20ms
03-11 20:24:00.278: D/LOOK(1386): 4
03-11 20:24:00.278: I/VALUE LIST(1386): [2, 5, 5, 3, 4, 3, 5, 5, 4] <<<<LIST HERE

但是这里是相同的代码但是在OnclickListener中:

 button.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    Random rand = new Random();
                    int rndInt = rand.nextInt(5)+ 1 ; 
                     list.add(rndInt);  
                      Log.d("VALUE", Integer.toString(rndInt));
                      Log.i("VALUE LIST ", list.toString()+" <<<<LIST HERE");
                }


            });

按钮按下按钮次数为3次:

03-11 20:21:26.858: D/LOOK(1216): 2
03-11 20:21:26.858: I/VALUE LIST(1216): [2] <<<<LIST HERE
03-11 20:21:28.288: D/LOOK(1216): 1
03-11 20:21:28.288: I/VALUE LIST(1216): [2, 1] <<<<LIST HERE
03-11 20:21:29.878: D/LOOK(1216): 2
03-11 20:21:29.878: I/VALUE LIST(1216): [2, 1, 2] <<<<LIST HERE

你可以看到哪种作品非常完美。如上所述,我需要使用ontouch,但为什么它会从单个按钮中添加多个值?我该怎么做才能解决这个问题。

1 个答案:

答案 0 :(得分:1)

if (event.getAction() == MotionEvent.ACTION_DOWN) { //add to your list here} 

很高兴我能帮忙!