Android Activity as a dialog

2018-12-31 06:17发布

I have an Activity named whereActity which has child dialogs as well. Now, I want to display this activity as a dialog for another activity.

How can I do that?

enter image description here

8条回答
步步皆殇っ
2楼-- · 2018-12-31 07:13

Use this code so that the dialog activity won't be closed when the user touches outside the dialog box:

this.setFinishOnTouchOutside(false);

requires API level 11

查看更多
与风俱净
3楼-- · 2018-12-31 07:15

If your activity is being rendered as a dialog, simply add a button to your activity's xml,

<Button
    android:id="@+id/close_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Dismiss" />

Then attach a click listener in your Activity's Java code. In the listener, simply call finish()

Button close_button = (Button) findViewById(R.id.close_button);
close_button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
});

That should dismiss your dialog, returning you to the calling activity.

查看更多
登录 后发表回答