gpt4 book ai didi

android - 如何为 PopupWindow 设置 OnTouchListener?

转载 作者:行者123 更新时间:2023-11-29 18:17:08 27 4
gpt4 key购买 nike

这是我的代码,当用户松开手指时触摸按钮并关闭它时显示弹出窗口。

但我需要知道用户是刚刚松开手指还是滑入弹出窗口并从那里松开手指。

在我的代码中,这实际上不起作用,因为我猜想如果用户不松开手指,弹出窗口就不会获得焦点。有办法解决吗?

谢谢!

    LinearLayout LinLay = new LinearLayout(this);
ImageView ivg = new ImageView(this);
ivg.setImageResource(R.drawable.icon);
LinLay.addView(ivg);
final PopupWindow pw = new PopupWindow(LinLay, 100, 100, true);

final Button bt2 = (Button)findViewById(R.id.button2);
bt2.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN )
{
pw.showAtLocation(bt2, Gravity.CENTER, 0, 0);
}
else if(event.getAction() == MotionEvent.ACTION_UP )
{
pw.dismiss();
}
return true;
}
});


ivg.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP )
{
showToast(); /// doesn't work! :)
}
return true;
}
});
}


public void showToast()
{
Toast.makeText(this, "AAA!", 500).show();
}

最佳答案

使用imageview和imageview onclick()显示showpopoup()窗口

im = new ImageView(this);
im.setImageResource(R.drawable.btn);

im.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showPopup(R.layout.popup_example);
}
});

private void showPopup(int resourses) {

final LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflate.inflate(resourses, null, false);
ViewGroup viewgroup = (ViewGroup) view
.findViewById(R.id.buttonContainer);
LinearLayout layout = new LinearLayout(this);

view.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
mPopup.dismiss();
return true;
}
});

mPopup = new PopupWindow(view, 160, LayoutParams.FILL_PARENT, true);
mPopup.setAnimationStyle(android.R.style.Animation_InputMethod);
mPopup.showAtLocation(view, Gravity.LEFT, 0, 10);
}

关于android - 如何为 PopupWindow 设置 OnTouchListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7526200/

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