其实我已经创建了一个按钮数组。现在我想要的是双击每个按钮我要显示一个吐司,显示点击的按钮的文本,我已经为singleClick做了但是我不知道怎么做双击。
我为按钮阵列编写的代码
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
//Create Button
for (int i = 0; i<6; i++)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout, param);
for(int j=0; j<7; j++)
{
m_pBtnDay[i][j] = new Button(this);
m_pBtnDay[i][j].setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
rowLayout.addView(m_pBtnDay[i][j], param);
m_pBtnDay[i][j].setTextSize(12);
//save button position
m_pBtnDay[i][j].setTag(new CalendarForm(i , j));
}
}
答案 0 :(得分:0)
我能想到的一种方法是使用手势检测
yourButton.setOnTouchListener(touchListener);
。
// Gesture detection
gestureDetector = new GestureDetector(new GestureListener());
View.OnTouchListener touchListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// The onTouch method has a View parameter which is a reference to the touched view. Cast
// this to Button to get its caption:
String caption=((Button)v).getText();
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
。
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
@Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
Log.d("Double Click", "Tapped at: (" + x + "," + y + ")");
return true;
}
}