到目前爲止,我已經搜索了幾個小時,並沒有找到任何適合我的問題的解決方案。onTouchListener for TableRow中的多個TextViews
我擺弄着我的android應用程序,並試圖做一個數獨遊戲。
TableLayout由9個TableRows組成,每個TableRows包含9個TextViews。 因此,9 * 9格包含81個TextViews各自具有OnTouchListener:
final OnTouchListener cellTouch = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
Log.d("TOUCH", "id: "+v.getId() + " "+e.toString());
final int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN: {
// set background to different colour
// set background back if this is different TextView
break;
}
case MotionEvent.ACTION_MOVE: {
break;
}
}
return true;
}
};
的OnTouch獲取在每個單獨的TextView燒製,但是觸摸之一,移動軌跡球圍繞以下後發生:
- ACTION_DOWN
- 數ACTION_MOVE的
- ACTION_UP
但是,我想要的是OnTouch在其他TextView上觸發並使其突出顯示。 我創建了一個單元的方式如下:
TextView cellLabel = (TextView) inflater.inflate(R.layout.sudoku_cell, tr, false);
cellLabel.setId(curCellId);
cellLabel.setText(""+ curCellId);
cellLabel.setOnTouchListener(cellTouch);
cellLabel.setOnFocusChangeListener(cellFocus);
cellLabel.setOnClickListener(cellClick);
cellLabel.setFocusable(true);
cellLabel.setClickable(true);
cellLabel.setFocusableInTouchMode(true);
每個單獨的小區:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="35dp"
android:layout_weight="1"
android:textSize="18dp"
android:textColor="#FFFFFF"
android:gravity="center"
android:background="@layout/border"
/>
我一直在嘗試很多不同的東西。比如確定TextView的指針位於頂部,但沒有任何運氣。
希望你們任何人都有一個主意。感謝您的時間。
親切的問候。
EDIT
通過TextView的[] [],並找到合適的小區搜索功能來循環。但是,只能在連續的基礎上工作。如果我從一行切換到另一行,它不起作用。
private TextView getCell (View v, MotionEvent e) {
float x = e.getX();
float y = e.getY();
for (int i = 0; i < GRID_HEIGHT; i++) {
for (int j = 0; j < GRID_WIDTH; j++) {
TextView tv = tvGrid[i][j];
Rect rectView = new Rect(tv.getLeft(), tv.getTop(), tv.getRight(), tv.getBottom());
if(rectView.contains((int)x, (int)y)) {
return tv;
}
}
}
return null;
}
一旦視圖已經表示將處理觸摸對方的意見不會得到事件,直到一個後續行動。看到亞歷克斯的回答如下,這幾乎是我過去做這項工作的方式 – Idistic
這正是發生的情況。將考慮亞歷克斯的建議。 – LordOfBones