gpt4 book ai didi

android - setOnLongClickListener 不工作

转载 作者:行者123 更新时间:2023-11-29 21:17:46 25 4
gpt4 key购买 nike

我正在使用第三方库。它使图像可拖动,并在您捏合和旋转时缩放。我正在尝试实现图像的删除,并制定了一种方法来删除您将手指放在上面的图像。现在我只需要调用它。我唯一成功调用它的地方是 onDraw。理想情况下,我会在 doubleTap 上调用它,因为用户会长按它来拖动它。但是 longClick 显然更容易,所以我首先测试了它。这是我的代码。滚动到底部以查看我的 setOnClickListener。我将向您展示 setOnClickListener 之前的类代码,以便为您提供一些背景信息:

public class PhotoSortrView extends View implements MultiTouchObjectCanvas<PhotoSortrView.Img> {

private ArrayList<Integer> mDrawables = new ArrayList<Integer>();// { R.drawable.m74hubble, R.drawable.catarina, R.drawable.tahiti, R.drawable.sunset, R.drawable.lake };

public void addDrawable(int drawable, Context context){
Resources res = context.getResources();
mDrawables.add(drawable);

mImages.add(new Img(drawable, res));
invalidate();
}

private ArrayList<Img> mImages = new ArrayList<Img>();
private AppUser user;

// --

private MultiTouchController<Img> multiTouchController = new MultiTouchController<Img>(this);

// --

private PointInfo currTouchPoint = new PointInfo();

private boolean mShowDebugInfo = true;

private static final int UI_MODE_ROTATE = 1, UI_MODE_ANISOTROPIC_SCALE = 2;

private int mUIMode = UI_MODE_ROTATE;

// --

private Paint mLinePaintTouchPointCircle = new Paint();

// ---------------------------------------------------------------------------------------------------

public PhotoSortrView(Context context) {
this(context, null);
}

public PhotoSortrView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public PhotoSortrView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}

@SuppressWarnings("deprecation")
private void init(Context context) {
Resources res = context.getResources();
for (int i = 0; i < mDrawables.size(); i++)
mImages.add(new Img(mDrawables.get(i), res));

mLinePaintTouchPointCircle.setColor(Color.YELLOW);
mLinePaintTouchPointCircle.setStrokeWidth(5);
mLinePaintTouchPointCircle.setStyle(Style.STROKE);
mLinePaintTouchPointCircle.setAntiAlias(true);
this.setLongClickable(true);

this.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
Toast.makeText(getContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
Log.d("long clicking", "long clicking");
return true;
}
});
}

我是 Android 的新手,所以可能不完全理解这个库类的上下文。我在我的 Activity 中这样称呼这个类:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dressing_room);
photoSorter = new PhotoSortrView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.addContentView(photoSorter, params);

它是一个被添加到这个 Activity 中的 View ,但它有一个 Canvas 并且可以添加很多可拖动的图像。谢谢。

编辑 我把它改成

this.setOnTouchListener(new View.OnTouchListener() {

// @Override
//public boolean onLongClick(View v) {
// Toast.makeText(getContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
// Log.d("long clicking", "long clicking");
// return true;
// }

@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
Log.d("long clicking", "long clicking");
return false;
}
});
}

那行得通。我们可以从那里得到 doubleTap 吗?

最佳答案

有一个内置的GestureDetector能够“捕获”设备与用户之间的所有常见交互,包括双击。然而,要为单个 View 或选择的 View 设置它,您必须做一些繁重的工作。我现在将发布我的测试 Activity 的代码:

public class MainActivity extends Activity implements OnClickListener {
private GestureDetector gd;
View.OnTouchListener otl;

class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
return true;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gd = new GestureDetector(this, new MyGestureDetector());
otl = new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return gd.onTouchEvent(event);
}
};
LinearLayout cotainerView = (LinearLayout) findViewById(R.id.container);
TextView tvNormal = new TextView(this);
tvNormal.setText("Normal text view");
tvNormal.setHeight(50);
DoubleTapTextView tvDoubleTap = new DoubleTapTextView(this);
tvDoubleTap.setText("Double tap text view");
tvDoubleTap.setHeight(50);
tvDoubleTap.setOnClickListener(this);
tvDoubleTap.setOnTouchListener(otl);
containerView.addView(tvNormal);
containerView.addView(tvDoubleTap);
}

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

}

所以诀窍是将 onTouch 事件及其参数 (MotionEvent e) 传递给您的自定义 GestureListener。这样,每个手势(触摸事件)都将被监听,但您的自定义监听器将仅在双击手势的情况下执行 onDoubleTap(MotionEvent e) 方法。希望您理解这背后的想法,作为奖励,我发布了在模拟器中运行的应用程序的屏幕截图,以向您展示该事件实际上已被捕获:) Screenshot

编辑:这里是解决方案的关键点

  1. Activity 实现 OnClickListener
  2. OnClickListerOnTouchListener 注册所需的 View

    tvDoubleTap.setOnClickListener(this); //Your activity implements OnClickListener
    tvDoubleTap.setOnTouchListener(otl); //The wrapper listener from which you call your custom one
  3. 在基本的 OnTouchListener 中,将参数传递给您的自定义 GestureListener

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    return gd.onTouchEvent(event);
    }
  4. 在您的自定义 GestureListener 的 onDoubleTap(MotionEvent e) 方法中,实现您的解决方案。

    class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onDoubleTap(MotionEvent e) {
    Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
    return true;
    }
    }

致谢:感谢 gav ,因为他对 his question 的研究

关于android - setOnLongClickListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21072957/

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