Shr*_*jan 150 android dialog android-emulator android-layout android-dialog
我为我的应用程序实现了一个自定义对话框.我希望实现当用户在对话框外单击时,对话框将被取消.我该怎么做?
use*_*305 343
如果dialog.setCanceledOnTouchOutside(true);
在对话框外触摸,可以使用哪个将关闭对话框.
就像是,
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);
Run Code Online (Sandbox Code Playgroud)
或者如果您的对话在非模型中,那么,
1 - FLAG_NOT_TOUCH_MODAL
为对话框的窗口属性设置标志
Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Run Code Online (Sandbox Code Playgroud)
2 - 向Windows属性添加另一个标志,FLAG_WATCH_OUTSIDE_TOUCH
- 这个用于对话框以在其可见区域之外接收触摸事件.
3 - 覆盖onTouchEvent()
对话框并检查操作类型.如果动作类型是' MotionEvent.ACTION_OUTSIDE
'表示,则用户正在对话区域外进行交互.因此,在这种情况下,您可以减少对话框或决定要执行的操作.查看plainprint?
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_OUTSIDE){
System.out.println("TOuch outside the dialog ******************** ");
this.dismiss();
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅如何根据触摸点关闭自定义对话框?当触摸外部对话区域时, 如何关闭非模态对话框
Luk*_*vak 16
您可以使用onTouchEvent的此实现.它可以防止在活动下面对触摸事件做出反应(如提到的howettl).
@Override
public boolean onTouchEvent ( MotionEvent event ) {
// I only care if the event is an UP action
if ( event.getAction () == MotionEvent.ACTION_UP ) {
// create a rect for storing the window rect
Rect r = new Rect ( 0, 0, 0, 0 );
// retrieve the windows rect
this.getWindow ().getDecorView ().getHitRect ( r );
// check if the event position is inside the window rect
boolean intersects = r.contains ( (int) event.getX (), (int) event.getY () );
// if the event is not inside then we can close the activity
if ( !intersects ) {
// close the activity
this.finish ();
// notify that we consumed this event
return true;
}
}
// let the system handle the event
return super.onTouchEvent ( event );
}
Run Code Online (Sandbox Code Playgroud)
资料来源:http://blog.twimager.com/2010/08/closing-activity-by-touching-outside.html
Ebi*_*ian 16
简单地使用
dialog.setCanceledOnTouchOutside(true);
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用样式xml中定义的主题自定义对话框,请将此行放在主题中:
<item name="android:windowCloseOnTouchOutside">true</item>
Run Code Online (Sandbox Code Playgroud)
dialog.setCanceledOnTouchOutside(true);
Run Code Online (Sandbox Code Playgroud)
在外面触摸时关闭对话框.
如果您不想在外面联系,请使用以下代码:
dialog.setCanceledOnTouchOutside(false);
Run Code Online (Sandbox Code Playgroud)
此方法应完全避免灰色区域下方检索点击事件的活动.
如果您拥有它,请删除此行:
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Run Code Online (Sandbox Code Playgroud)
把它放在你创建的活动上
getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
Run Code Online (Sandbox Code Playgroud)
然后用这个覆盖触摸事件
@Override
public boolean onTouchEvent(MotionEvent ev)
{
if(MotionEvent.ACTION_DOWN == ev.getAction())
{
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
// You have clicked the grey area
displayYourDialog();
return false; // stop activity closing
}
}
// Touch events inside are fine.
return super.onTouchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)
此代码用于当使用单击对话框时隐藏软输入以及当用户单击对话框外侧时软输入和对话框都关闭时。
dialog = new Dialog(act) {
@Override
public boolean onTouchEvent(MotionEvent event) {
// Tap anywhere to close dialog.
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (!dialogBounds.contains((int) event.getX(),
(int) event.getY())) {
// You have clicked the grey area
InputMethodManager inputMethodManager = (InputMethodManager) act
.getSystemService(act.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(dialog
.getCurrentFocus().getWindowToken(), 0);
dialog.dismiss();
// stop activity closing
} else {
InputMethodManager inputMethodManager = (InputMethodManager) act
.getSystemService(act.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(dialog
.getCurrentFocus().getWindowToken(), 0);
}
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
你可以试试这个:-
AlterDialog alterdialog;
alertDialog.setCanceledOnTouchOutside(true);
Run Code Online (Sandbox Code Playgroud)
或者
alertDialog.setCancelable(true);
Run Code Online (Sandbox Code Playgroud)
如果你有AlterDialog.Builder
那么你可以试试这个:-
alertDialogBuilder.setCancelable(true);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
139193 次 |
最近记录: |