gpt4 book ai didi

java - OntouchListener 向数组添加太多值

转载 作者:行者123 更新时间:2023-12-01 11:48:06 26 4
gpt4 key购买 nike

对于我正在开发的应用程序,我需要使用 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,但为什么它会通过按一次按钮添加多个值?我能做些什么来解决这个问题。

最佳答案

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

很高兴我能提供帮助!

关于java - OntouchListener 向数组添加太多值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28996668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com