[英]Android : Buttons in the background Activity of a 'Dialog' is active?
I have an Activity which has an 'OK' button. 我有一个带有“确定”按钮的活动。 And I have an 'Edit' button which will open a Dialog (
theme="@android:style/Theme.Holo.Light.Dialog"
). 我有一个“编辑”按钮,它将打开一个对话框(
theme="@android:style/Theme.Holo.Light.Dialog"
)。 When I am in the EditDialog, I can see the OK button in the background Activity, and I can press that, and it the press is getting registered. 当我进入EditDialog时,我可以在后台活动中看到“确定”按钮,并且可以按该按钮,并且该按钮正在注册。
Is there a way to disable the background Activity actions when a Dialog is open? 打开对话框时,是否可以禁用后台活动动作? ie I want to modify things in the Dialog alone.
即我只想修改对话框中的内容。
Edit: Adding a sample code which shows this behaviour. 编辑:添加示例代码,以显示此行为。 Main Activity:
主要活动:
public class DialogTestActivity extends Activity implements OnClickListener {
private final String TAG = "DialogTest.main";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.button_open)).setOnClickListener(this);
((Button) findViewById(R.id.button_ok)).setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_ok:
Log.w(TAG, "OK Button Pressed!");
break;
case R.id.button_open:
Log.d(TAG, "Opening new Window.");
Intent intent = new Intent(this, TestDialog.class);
startActivity(intent);
default:
break;
}
}
}
TestDialog 'dialog': TestDialog“对话框”:
public class TestDialog extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_dialog);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
Window window = this.getWindow();
window.setAttributes((android.view.WindowManager.LayoutParams) params);
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
}
}
Manifest: 表现:
<activity
android:name=".DialogTestActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TestDialog"
android:label="@string/dialog_label"
android:theme="@android:style/Theme.Holo.Light.Dialog" >
</activity>
With the above code, when the TestDialog was open, the Button Press on the background Activity were registered - OK Button Pressed!
使用上面的代码,当打开TestDialog时,将注册后台活动上的Button Press-
OK Button Pressed!
will be logged. 将被记录。
First, this is not a dialog. 首先,这不是对话框。 This is a dialog-themed activity.
这是一个以对话为主题的活动。 A dialog inherits from
Dialog
. 对话框继承自
Dialog
。
Second, your use of WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
is giving you precisely the behavior that you do not want. 其次,对
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
的使用恰恰为您提供了您不想要的行为。 Delete this line of code, and things should work better. 删除此行代码,事情应该会更好。
Nothing in the underlying Activity
should be clickable when a Dialog
is in the foreground (this is how Dialog
s in Android work). 当
Dialog
位于前台时,基础Activity
中的任何内容均不可单击(这是Android中Dialog
的工作方式)。 If you are able to interact with the underlying Activity
when the Dialog
is open, then there is probably something wrong with your implementation. 如果您可以在打开
Dialog
时与基础Activity
交互,那么您的实现可能存在问题。
Edit: 编辑:
Remove this line and all should work: 删除此行,一切都应该工作:
this.setCanceledOnTouchOutside(false);
如果您的活动是通过设置对话框主题打开的,请使用以下代码
setFinishOnTouchOutside(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.