我正在使用带有自定义视图的Toast.我实例化视图并在toast上调用setView.Toast应该漂浮在最顶层,而不是接收焦点和触摸事件,它运作良好.应用程序启动后,用户抱怨说,在Galaxy Note等几款手机型号上,Toast确实获得了触摸事件,而下面的应用程序却没有.
我打印了视图在方法setLayoutParams中获取的布局参数标志(WindowManager.LayoutParams).事实证明,在大多数设备上,值为0x000098,但在某些设备上为0x040088.在Toast获取触摸事件的设备上,删除标志FLAG_NOT_TOUCHABLE,并添加标志FLAG_WATCH_OUTSIDE_TOUCH.这解释了为什么吐司获得触摸事件.
但是什么导致了这种差异?有没有办法迫使吐司不可触摸?
我有一个解决方法。您需要能够访问 toast 所覆盖的视图。您基本上可以捕获发送到 Toast 的触摸事件。然后更改 MotionEvent X 和 Y。然后将 MotionEvent 发送到被遮挡的视图。对我来说这感觉有点老套,但它确实有效。我认为更改吐司上的标志会更好,但我找不到它们。
在活动中创建 Toast:
final Taost toast = Toast.makeText(this, "Text", Toast.LENGTH_LONG);
final View myCoveredView = findViewById(R.id.my_covered_view);
final View toastView = toast.getView();
toastView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View v2, final MotionEvent event) {
//Make sure the view is accessible
if(myCoveredView != null){
//Set the touch location to the absolute screen location
event.setLocation(event.getRawX(), event.getRawY());
//Send the touch event to the view
return myCoveredView.onTouchEvent(event);
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
如果有人有更好的方法,我很乐意在这里。
| 归档时间: |
|
| 查看次数: |
1460 次 |
| 最近记录: |