安卓:如何读取按钮触摸表? [英] Android: How to read button touches in table?
本文介绍了安卓:如何读取按钮触摸表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是在按钮(自定义键盘)的表来测试按钮触摸的正确方法?对于每个按钮OnTouch听众似乎很麻烦。
What's the correct way to test for button touches in a table of buttons (custom keypad)? OnTouch Listeners for every button seems cumbersome.
推荐答案
这不是累赘。不过,我会建议使用同一个侦听为他们所有。的(这是使用 OnTouchListener
; OnClickListener
可能是你的情况更好)的
It's not cumbersome. However I would suggest using the same listener for all of them. (And this is using OnTouchListener
; OnClickListener
may be better in your case.)
public class MyButtonTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent ev) {
Button b = (Button) v;
String buttonText = b.getText().toString();
// Do whatever, based on buttonText. Example: if (buttonText.equals("#")) { doPound(); }
}
}
然后,创建这些项目中的一个,并将其分配给所有的人。
Then, you create one of these items, and assign it to all of them.
MyButtonTouchListener listener = new MyButtonTouchListener();
ArrayList<Button> myButtons = new ArrayList<Button>();
// Add all your Button objects to the ArrayList.
for (Button b : myButtons)
b.setOnTouchListener(listener);
现在,您的听众只有一次在内存中出现,并且每个项目仍具有独特的功能。
Now, your listener only appears once in memory, and each item still has unique functionality.
这篇关于安卓:如何读取按钮触摸表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文