我有一個對話框,我有很多活動...我需要在所有活動中調用相同的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. 自定義我的應用程序中的所有對話框
- 2. 在應用程序中的所有對話框的自定義主題
- 3. Android應用程序自定義對話框體系結構
- 4. 在Eclipse應用程序中自定義對話框
- 5. 用於Web應用程序的Windows Mobile自定義對話框?
- 6. 在android中的自定義對話框?
- 7. 如何在android應用程序中使用自定義字體?
- 8. 如何在我的Android自定義應用程序類中顯示對話框片段?
- 9. 如何在我的應用程序中使用自定義尺寸在android
- 10. 帶自定義進度對話框的應用程序崩潰
- 11. 自定義對話框的setText()導致應用程序崩潰
- 12. 木衛四自定義對話框自定義樣式的Windows應用程序
- 13. 在我的android應用程序中的ANR對話框
- 14. android自定義對話框
- 15. Android自定義對話框
- 16. Android自定義對話框
- 17. 自定義對話框Android
- 18. 取代facebook對話框中的自定義對話框在android
- 19. 如何在Android中自定義ANR對話框而不是android ANR對話框
- 20. 我想在我的android應用程序中添加我自己的樣式彈出對話框,如圖所示
- 21. 更改ANR(應用程序無響應)對話框(Android)的自定義對話框
- 22. 在我的自定義對話框中使用WixUIBannerBmp
- 23. Facebook自定義應用程序請求對話框
- 24. Gluon Mobile對話框應用程序自定義
- 25. 在自定義對話框類中使用Android註釋
- 26. 使用ListView在自定義對話框中(android)
- 27. 在Android中使用自定義對話框顯示活動
- 28. 如何在沒有對話框的情況下鎖定應用程序android
- 29. Android中的對話框中的指定應用程序的GridView
- 30. 如何在Android應用程序中創建進度對話框?
你相信靜態方法嗎? – ingsaurabh
是的!!!!!!!!!!!! –
創建自定義對話框相當棘手,但這個鏈接將有所幫助http://developer.android.com/guide/topics/ui/dialogs.html –