用于所有其他活动的干净对话框收集器类 [Android]

发布于 2024-11-26 14:34:21 字数 2134 浏览 4 评论 0原文

我正在尝试并寻找一种好的方法来处理单个类中的对话框,以便我可以在我想要的任何活动中使用它们。最干净、性能最好的方法是最好的。 目前,我在完成主要工作的每个活动中轻松进行对话。如果我需要更改对话框或对话框事件,则搜索所有类会很麻烦。

[已解决] ~ 使用下面的代码更新。

看起来很棒。希望我能做得很好。有什么优化吗?

--- 对话框类中的代码

public class Dialogs extends Activity {
    public static final int DIALOG_START = 0;
    public static final int DIALOG_END = 1;

    private Context mContext;
    private int mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getApplicationContext();
        mDialog = getIntent().getExtras().getInt("dialog");
        showDialog(mDialog);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch (id) {
        case DIALOG_START:
            Toast.makeText(mContext, "Test...", Toast.LENGTH_SHORT).show();
            finish(); //works because toasts are somehow delayed
            break;
        case DIALOG_END:
            // do something else but always finish(), e.g. after dialogbutton- click.
            break;
        }
        return dialog;
    }

}

--- 目标 Activity 中的代码(例如按钮单击):

Intent dialogIntent = new Intent();
dialogIntent.setClass(Main.this, Dialogs.class);
dialogIntent.putExtra("dialog" , Dialogs.DIALOG_START);
startActivityForResult(dialogIntent, 0x0);

--- 清单中的代码:

<activity android:name=".Dialogs" android:label="@string/app_name"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@style/dialog" />

- -- 样式文件中的代码 (values/style.xml):

<style name="dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Im trying and searching for a good method to handle dialogs in a single class so i can use them in any activity i want. The most clean and good performance method would be best.
Currently im handly dialogs in each activity where the main work is done. If i need to change a dialog or dialogevent its a hassle to search through all my classes.

[SOLVED] ~ Update with code below.

Looks great. Hope im doing good with it. Any optimization?

--- Code from Dialog Class

public class Dialogs extends Activity {
    public static final int DIALOG_START = 0;
    public static final int DIALOG_END = 1;

    private Context mContext;
    private int mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getApplicationContext();
        mDialog = getIntent().getExtras().getInt("dialog");
        showDialog(mDialog);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch (id) {
        case DIALOG_START:
            Toast.makeText(mContext, "Test...", Toast.LENGTH_SHORT).show();
            finish(); //works because toasts are somehow delayed
            break;
        case DIALOG_END:
            // do something else but always finish(), e.g. after dialogbutton- click.
            break;
        }
        return dialog;
    }

}

--- Code in target Activity (for example button click):

Intent dialogIntent = new Intent();
dialogIntent.setClass(Main.this, Dialogs.class);
dialogIntent.putExtra("dialog" , Dialogs.DIALOG_START);
startActivityForResult(dialogIntent, 0x0);

--- Code in Manifest:

<activity android:name=".Dialogs" android:label="@string/app_name"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@style/dialog" />

--- Code in Stylefile (values/style.xml):

<style name="dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-12-03 14:34:21

您必须创建一个 Activity,其主题在 Android-manifest.xml 中设置为 @android:style/Theme.Dialog。然后,您可以在任何地方使用此对话框(或弹出窗口),就像我们使用 startActivityForResult(Intent Intent, int requestCode) 的活动一样。

You have to create one Activity whose theme set as @android:style/Theme.Dialog in Android-manifest.xml. Then you can use this one as dialog(or popup) anywhere simply as we use activities that is by startActivityForResult(Intent intent, int requestCode).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文