Android 显示对话框时禁用“我的活动”中的屏幕旋转
我有一个对话框片段,在我的应用程序执行任务时显示 我取消正在调用activitiesAndroid 显示对话框时禁用“我的活动”中的屏幕旋转,android,android-dialog,android-dialogfragment,android-popupwindow,Android,Android Dialog,Android Dialogfragment,Android Popupwindow,我有一个对话框片段,在我的应用程序执行任务时显示 我取消正在调用activitiesonDestroy()方法时执行的任务 如果用户在弹出窗口显示时旋转,android会销毁该活动,然后重新绘制。因此,我已经告诉它取消ondestory()方法中的任务。然后它停止执行任务,弹出窗口停留在那里。因为它通常只有在任务完成后才会被删除 我知道这可能不是最好的方法,但我觉得这将是一个快速修复我的问题,它不会导致任何其他问题(我知道) 我想在弹出窗口显示之前禁用应用程序的旋转,然后在对话框变暗时重新启用它
onDestroy()
方法时执行的任务
如果用户在弹出窗口显示时旋转,android会销毁该活动,然后重新绘制。因此,我已经告诉它取消ondestory()方法中的任务。然后它停止执行任务,弹出窗口停留在那里。因为它通常只有在任务完成后才会被删除
我知道这可能不是最好的方法,但我觉得这将是一个快速修复我的问题,它不会导致任何其他问题(我知道)
我想在弹出窗口显示之前禁用应用程序的旋转,然后在对话框变暗时重新启用它
我将如何通过编程实现这一点
像这样:
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u横向)代码>
那我在哪里做呢?在对话框的onCreate()
中?或者在我从活动中调用对话框之前?尝试这样做,并讲述您的体验!
当您调用onDestroy()的实现时,请使用
我认为如果这不起作用的话,就很接近了。无论如何,你应该看看
.
我希望这对你有帮助 尝试为对话框和处理方向实现onDismiss和onShow侦听器。例如,执行以下操作:
public class MyActivity extends Activity
implements DialogInterface.OnDismissListener,DialogInterface.OnShowListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog alertDialog = new
AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setOnShowListener(this);
alertDialog.setOnDismissListener(this);
alertDialog.show();
}
@Override
public void onShow(DialogInterface dialog) {
final int screenOrientation = ((WindowManager) getbaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
switch (screenOrientation){
case Surface.ROTATION_180:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_0:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
}
@Override
public void onDismiss(DialogInterface dialog) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
}
我希望这对某些人有所帮助。试试这个怎么样:类型OrientationListener已被弃用,这不会给我带来任何问题吗?哎呀,我的错,请改用。我不确定这是不是你想要的方式。我应该试试看。。。总之,有人在上面贴了一些有趣的东西
public class MyActivity extends Activity
implements DialogInterface.OnDismissListener,DialogInterface.OnShowListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog alertDialog = new
AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setOnShowListener(this);
alertDialog.setOnDismissListener(this);
alertDialog.show();
}
@Override
public void onShow(DialogInterface dialog) {
final int screenOrientation = ((WindowManager) getbaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
switch (screenOrientation){
case Surface.ROTATION_180:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_0:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
}
@Override
public void onDismiss(DialogInterface dialog) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
}