gpt4 book ai didi

android - 如何制作特殊区域以调用触摸事件?

转载 作者:行者123 更新时间:2023-11-30 04:39:58 24 4
gpt4 key购买 nike

我的程序中有下一个 UI:ui .当然,我想在一组按钮上处理手指滑动事件,而不会失去单击按钮的能力。如何做到这一点?

最佳答案

你是说屏幕底部的按钮吗?

这是一个示例,其中有 3 个水平对齐的按钮,可以在其中滑动和点击。处理“onSingleTapUp”中的点击和“onFling()”中的滑动。

public class SwipeExample extends Activity implements OnTouchListener {

private static final String TAG = SwipeExample.class.getName();

private int mOnTouchId;

private GestureDetector mGestureDetector;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mGestureDetector = new GestureDetector(new MyGestureDetector());

Button one = (Button) findViewById(R.id.button_one);
one.setOnTouchListener(this);
Button two = (Button) findViewById(R.id.button_two);
two.setOnTouchListener(this);
Button three = (Button) findViewById(R.id.button_three);
three.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
mOnTouchId = v.getId();
if (mGestureDetector.onTouchEvent(event)) {
Log.d(TAG, "onTouchEvent");
}
return false;
}

private class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.d(TAG, "onFling() - MyGestureDetector");
return true;
}
@Override
public boolean onDown(MotionEvent e) {
Log.d(TAG, "onDown() - MyGestureDetector");
return true;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d(TAG, "onSingleTapUp() - MyGestureDetector");
if (mOnTouchId == R.id.button_one) {
Log.d(TAG, "Button one");
} else if (mOnTouchId == R.id.button_two) {
Log.d(TAG, "Button two");
} else if (mOnTouchId == R.id.button_three) {
Log.d(TAG, "Button three");
}
return true;
}
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/LinearLayout"
android:background="#348282">
<Button android:id="@+id/button_one" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button one" />
<Button android:id="@+id/button_two" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button two" />
<Button android:id="@+id/button_three" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button three" />
</LinearLayout>

关于android - 如何制作特殊区域以调用触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6163485/

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