2012-12-03 46 views
1

我在一個活動中創建了一個對話框。使用異步任務,我會定期顯示該對話框。當我移動到另一個活動時是否可以顯示對話框?如何在多個活動中顯示相同的對話框?

+0

檢查這裏我的答案.... http://stackoverflow.com/questions/13561915/single-alert-dialog-for-entire-application/13561965# 13561965 –

回答

0

你可以製作一個按鈕來顯示對話框,在事件onclick顯示對話框的任何代碼,並設置按鈕點擊定期。

public void getRunningClick(){ 
    new Handler().postDelayed(new Runnable() { 
      public void run() { 
        //your code showing dialog 
      } 
     },(2*1000)); 
} 

希望對你的幫助

+0

假設代碼在Avtivity A中,當我移動到Acitivity B時,對話框是否會打開? – Vignesh

+0

你在獨立的類中創建這個方法,並且每個類,例如類A和類B調用這個方法。 – pwcahyo

+0

不,我不知道用戶在哪個活動中,我需要找到活動類並需要顯示它。 – Vignesh

1

我做到了,在過去的2分不同的方式。

1)創建一個佈局,用作需要時顯示的對話框(導入到每個活動佈局中並隱藏)(也可以創建一個'空'活動,只彈出對話框並且如果需要該消息。 2)創建一個CustomDialog類並使用它(我用它來處理自定義字體,但是我只在這個代碼中放一次)。

//main Activity: 
    DialegError da = new DialegError(this); 
    da.crearDialeg("APP ERROR", "this is an error"); 

//Error class 
public class DialegError { 
    private Activity a = null; 

    public DialegError(Activity activity){ 
      a=activity; 
    } 

    /** 
    * Default NO-MESSAGE errorDialog 
    */ 
    public void crearDialeg(String titol){ 
     AlertDialog dialog = new AlertDialog.Builder(a) 
      .setTitle(titol) 
//   .setIcon(R.drawable.) 
      .setPositiveButton(a.getString(R.string.button_aceptar), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        return; 
       } 
      }) 
      .show(); 
    } 

    /** 
    * Default errorDialog 
    */ 
    public void crearDialeg(String titol, String cos){ 
     AlertDialog dialog = new AlertDialog.Builder(a) 
      .setTitle(titol) 
      .setMessage(cos) 
//   .setIcon(R.drawable.) 
      .setPositiveButton(a.getString(R.string.button_aceptar), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        return; 
       } 
      }) 
      .show(); 

    //Personalized font. No way to deal with the title text. 
    Typeface font=Typeface.createFromAsset(a.getAssets(),"fonts/font_name.ttf"); 
    TextView textView = (TextView)dialog.findViewById(android.R.id.message); 
    textView.setTypeface(font); 
    textView = (TextView)dialog.findViewById(android.R.id.button1); 
    textView.setTypeface(font); 
    } 

    /** 
    * Error Dialog that closes the invoker activity. 
    */ 
    public void crearDialegError(String titol, String cos, int err){ 
     final Activity activitat = a; 
     final int error = err; 
     AlertDialog dialog = new AlertDialog.Builder(activitat) 
      .setTitle(titol) 
      .setMessage(cos) 
//   .setIcon(R.drawable.) 
      .setPositiveButton(activitat.getString(R.string.button_aceptar), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        activitat.setResult(error, new Intent()); 
        activitat.finish(); 
       } 
      }) 
      .show(); 
    } 
} 
相關問題