我有一个对话框,我有很多活动...我需要在所有活动中调用相同的dailog ...目前我已经在每个代码中编写代码活动的,但这是不正确的做法......任何一个可以帮我在这如何在我的所有应用程序中使用自定义对话框android
0
A
回答
0
下面好吧是我实现静态类对话框它,所以我可以从任何活动的时候调用它,我需要
方式public static void showProgressDialog(Context mContext, String text, boolean cancellable)
{
removeDialog();
mDialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater mInflater = LayoutInflater.from(mContext);
View layout = mInflater.inflate(R.layout.popup_example, null);
mDialog.setContentView(layout);
TextView mTextView = (TextView) layout.findViewById(R.id.text);
if (text.equals(""))
mTextView.setVisibility(View.GONE);
else
mTextView.setText(text);
mDialog.setOnKeyListener(new DialogInterface.OnKeyListener()
{
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK:
return true;
case KeyEvent.KEYCODE_SEARCH:
return true;
}
return false;
}
});
mDialog.setCancelable(cancellable);
mDialog.show();
}
public static void removeDialog()
{
if (mDialog != null)
mDialog.dismiss();
}
+0
你能告诉你如何在你的活动中调用它吗? – Yuri
1
只需创建一个类并在其中创建一个静态方法(如displayDialog),并在此范围内复制粘贴显示对话框的代码。现在在你的项目的任何地方调用这个静态方法。但是您可能必须将调用活动的上下文传递给静态方法。
public class dialogClass {
static Dialog dialog=null;
public static void exitApp_Dialog(Context context,String title,String message){
AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
alertbox.setTitle("Warning");
alertbox.setMessage("Exit Application?");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
activity.finish();
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertbox.show();
}
}
0
您应该重新组合在帮助类中构建Dialog
的代码。
下面是我为自己构建的DialogHelper
的摘录,我用它来显示我的应用程序的帮助文件。
public class DialogHelper {
public static AlertDialog showHelp(final Context ctx, final int resTitle, final int resFilename, final int resOk, final int resViewOnline, final int resOnlineUrl) {
final WebView webview = new WebView(ctx);
webview.loadUrl(ctx.getString(resFilename));
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(resTitle)
.setView(webview)
.setCancelable(false)
.setPositiveButton(resOk, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
})
.setNegativeButton(resViewOnline, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final Uri uri = Uri.parse(ctx.getString(resOnlineUrl));
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
ctx.startActivity(intent);
}
}
);
final AlertDialog dlg = builder.create();
dlg.show();
return dlg;
}
}
... other kinds of dialog take place here ...
}
这样,我只是叫
DialogHelper.showHelp(context, R.string.helpTitle, R.string.localizedFilename, R.string.labelOk, R.string.labelViewOnlineHelp, R.string.onlineHelpUrl);
从我所有的应用程序
。这是很多参数,但这是可行的。
该代码中有一个不好的东西:我使用setNegativeButton()
来实现其他目的。这是我应该重构的东西,但这不会改变方法中的任何内容。
关于showHelp()
的参数:它们是final
,因为它们用于方法中构建的匿名类。这是编译器的要求。
相关问题
- 1. 在Eclipse应用程序中自定义对话框
- 2. 用于Web应用程序的Windows Mobile自定义对话框?
- 3. 在android中的自定义对话框?
- 4. 如何在我的Android自定义应用程序类中显示对话框片段?
- 5. 自定义对话框Android
- 6. Android自定义对话框
- 7. android自定义对话框
- 8. Android自定义对话框
- 9. 取代facebook对话框中的自定义对话框在android
- 10. 在我的android应用程序中的ANR对话框
- 11. 我想在我的android应用程序中添加我自己的样式弹出对话框,如图所示
- 12. 更改ANR(应用程序无响应)对话框(Android)的自定义对话框
- 13. 在我的自定义对话框中使用WixUIBannerBmp
- 14. Facebook自定义应用程序请求对话框
- 15. 在Android中使用自定义对话框显示活动
- 16. 使用ListView在自定义对话框中(android)
- 17. 在自定义对话框的Android
- 18. 如何在没有对话框的情况下锁定应用程序android
- 19. 如何在Android应用程序中防止choosePrivateKeyAlias对话框?
- 20. 如何在Android应用程序中创建进度对话框
- 21. 在我的应用程序中调用自定义Android键盘
- 22. Android使用自定义对话框中的水平进度条
- 23. 使用自定义对话框时无法使用onDismiss() - Android
- 24. 如何使用我的自定义记录器在Android应用程序
- 25. 如何在android中获取自定义对话框的大小?
- 26. 如何在Android上自定义对话框标题的中心
- 27. 如何应用自定义微调图像进度对话框中的Android
- 28. 如何在自定义对话框
- 29. 我在vs2010中没有应用程序文件对话框
- 30. Android的自定义对话框的EditText
你相信静态方法吗? – ingsaurabh
是的!!!!!!!!!!!! –
创建自定义对话框相当棘手,但这个链接将有所帮助http://developer.android.com/guide/topics/ui/dialogs.html –