用于所有其他活动的干净对话框收集器类 [Android]
我正在尝试并寻找一种好的方法来处理单个类中的对话框,以便我可以在我想要的任何活动中使用它们。最干净、性能最好的方法是最好的。 目前,我在完成主要工作的每个活动中轻松进行对话。如果我需要更改对话框或对话框事件,则搜索所有类会很麻烦。
[已解决] ~ 使用下面的代码更新。
看起来很棒。希望我能做得很好。有什么优化吗?
--- 对话框类中的代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须创建一个 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
inAndroid-manifest.xml
. Then you can use this one as dialog(or popup) anywhere simply as we use activities that is bystartActivityForResult(Intent intent, int requestCode)
.