gpt4 book ai didi

android - 我应该在 android 中使用什么类型的事件监听器?

转载 作者:行者123 更新时间:2023-11-30 03:48:58 25 4
gpt4 key购买 nike

我有一个 5x5 的网格(我在 Canvas 上画了一个矩阵)。我已经在网格中安排了 TextView 按照数组的顺序......即(5X5)数组。问题是我想在水平或垂直滑动时更改 TextView 的值。

假设我在屏幕上连续使用从 [4][0] 到 [4][2] 的触摸移动,我想在这些地方修改 TextView 的值。

有什么想法吗?

最佳答案

这段代码可能对您有帮助:

public class MyClass extends Activity implements OnGestureListener{
ArrayList<TextView> tvs = new ArrayList<TextView>();
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.mygrid);
}

public void onResume() {
super.onResume();
GestureOverlayView gd = (GestureOverlayView)findViewById(R.id.gd);
LinearLayout g = (LinearLayout)findViewById(R.id.mygrid);
gd.addOnGestureListener(this);
g.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 5; i++) {
LinearLayout row = new LinearLayout(this);
g.addView(row);
row.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rowlp = (LayoutParams) row.getLayoutParams();
rowlp.width = LayoutParams.MATCH_PARENT;
rowlp.weight = 1;
rowlp.height = LayoutParams.WRAP_CONTENT;
row.setLayoutParams(rowlp);
for (int j = 0; j < 5; j++) {
TextView tv = new TextView(this);
row.addView(tv);
LinearLayout.LayoutParams lp = (LayoutParams) tv.getLayoutParams();
lp.height = LayoutParams.MATCH_PARENT;
lp.width = 0;
lp.weight = 1;
tv.setLayoutParams(lp);
tv.setText(String.valueOf(i * 5 + j));
tv.setTag(String.valueOf(i * 5 + j));
tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
tvs.add(tv);
if ((i * 5 + j) % 2 == 0)
tv.setBackgroundColor(0xff888888);
}
}
}

private boolean isPointInsideView(float x, float y, View view){
int location[] = new int[2];
view.getLocationOnScreen(location);
int viewX = location[0];
int viewY = location[1];

//point is inside view bounds
if(( x > viewX && x < (viewX + view.getWidth())) &&
( y > viewY && y < (viewY + view.getHeight()))){
return true;
} else {
return false;
}
}

@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
float x = event.getRawX();
float y = event.getRawY();
for (TextView v : tvs) {
if (isPointInsideView(x, y, v)) {
v.setText("HERE");
}
}
}

@Override
public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {

}

@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {

}

@Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {

}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gd"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/mygrid"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.gesture.GestureOverlayView>

希望对您有所帮助。

Edit2:用这些方法替换上面的方法以显着提高性能:

private TextView getAbove(float x, float y) {
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (isPointInsideView(x, y, tvs[i][j]))
return tvs[i][j];
return null;
}

@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
float x = event.getRawX();
float y = event.getRawY();
if (last == null || !isPointInsideView(x, y, last)) {
last = getAbove(x,y);
draw();
}
}

private void draw() {
if (last == null)
return;
last.setText("+");
}
private void reset() {
last = null;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
tvs[i][j].setText(String.valueOf(i * 5 + j));
}

@Override
public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
reset();
}
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
reset();
}
@Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
last = getAbove(event.getRawX(), event.getRawY());
draw();
}

TextView[][] tvs = new TextView[5][5];
TextView last = null;

并将 arraylist 变成 TextView[5][5]

关于android - 我应该在 android 中使用什么类型的事件监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460886/

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