2013-02-26 136 views
1

我創建了一個自定義對話框,創建一個標準對話框,我可以在一行之後創建一個標準對話框,這將是標準對話框。隨着參數我改變了文字。該對話框非常簡單,只有一個按鈕。自定義對話框等待響應

現在我正在重新思考這是一個好主意。我真的希望我的應用程序停止,直到我的對話框顯示。我怎麼能管理它?給對話框返回類型?或者,還有更好的方法?

我的對話框:

/** 
* custom dialog 
* 
* @param mcontext use activityname.this 
* @param title 
* @param text 
* @param button 
*/ 
public void showDialog(Context mcontext, String title,String text, String button) { 
    // fonts 
    Typeface tf_hn = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneue.ttf"); 
    Typeface tf_hn_bold = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneuebd.ttf"); 
    Resources res = mcontext.getResources(); 

    // custom dialog 
    final Dialog dialog = new Dialog(mcontext); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //not the normal dialog title 
    dialog.setContentView(R.layout.view_dialog); 

    TextView tv_dialog_title = (TextView) dialog.findViewById(R.id.tv_dialog_title); 
    tv_dialog_title.setText(title); 
    tv_dialog_title.setTypeface(tf_hn_bold); 
    tv_dialog_title.setTextColor(res.getColor(R.color.white)); 

    TextView tv_dialog_text = (TextView) dialog.findViewById(R.id.tv_dialog_text); 
    tv_dialog_text.setText(text); 
    tv_dialog_text.setTypeface(tf_hn); 
    tv_dialog_text.setTextColor(res.getColor(R.color.white)); 

    Button dialogButton = (Button) dialog.findViewById(R.id.bt_dialog_button); 
    dialogButton.setTypeface(tf_hn_bold); 
    dialogButton.setText(button); 
    dialogButton.setTextColor(res.getColor(R.color.white)); 
    // if button is clicked, close the custom dialog 
    dialogButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 

    dialog.show(); 
} 

,然後我可以用這樣的:

dialogH.showDialog(LoginActivity.this, res.getString(R.string.txt_dialog_fout), res.getString(R.string.txt_dialog_not_connected),res.getString(R.string.txt_dialog_button)); 

它的工作都很好,直到我想,以顯示與「你登錄」對話框(左右),然後在點擊顯示後開始一個意圖。任何人的想法?

回答

3

用這樣的內置偵聽器創建一個自定義對話框類。

public class MyDialog extends Dialog { 
    String title; 
    String text; 
    String button; 

    DialogListener listener; 

    interface DialogListener { 
     void onCompleted(); 

     void onCanceled(); 
    } 

    public MyDialog(Context context, String title, String text, String button) { 
     super(context); 
     this.title = title; 
     this.text = text; 
     this.button = button; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Typeface tf_hn = Typeface.createFromAsset(getContext().getAssets(), "helveticaneue.ttf"); 
     Typeface tf_hn_bold = Typeface.createFromAsset(getContext()..getAssets(), "helveticaneuebd.ttf"); 
     Resources res = getContext().getResources(); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); // not the normal dialog title 
     setContentView(R.layout.view_dialog); 

     TextView tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title); 
     tv_dialog_title.setText(title); 
     tv_dialog_title.setTypeface(tf_hn_bold); 
     tv_dialog_title.setTextColor(res.getColor(R.color.white)); 

     TextView tv_dialog_text = (TextView) findViewById(R.id.tv_dialog_text); 
     tv_dialog_text.setText(text); 
     tv_dialog_text.setTypeface(tf_hn); 
     tv_dialog_text.setTextColor(res.getColor(R.color.white)); 

     Button dialogButton = (Button) findViewById(R.id.bt_dialog_button); 
     dialogButton.setTypeface(tf_hn_bold); 
     dialogButton.setText(button); 
     dialogButton.setTextColor(res.getColor(R.color.white)); 
     // if button is clicked, close the custom dialog 
     dialogButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(listener != null) 
        listener.onCompleted(); 
       MyDialog.this.dismiss(); 
      } 
     }); 

     setOnCancelListener(new OnCancelListener() { 

      @Override 
      public void onCancel(DialogInterface dialog) { 
       if(listener != null) 
        listener.onCanceled(); 
      } 
     }); 
    } public void setDialogListener(DialogListener listener) { 
     this.listener = listener; 
    } 

} 

並以推行對話框:

MyDialog dialog = new MyDialog(getContext(), title, text, button); 
    dialog.setDialogListener(new MyDialog.DialogListener() { 

     @Override 
     public void onCompleted() { 
      // do stuff when dialog is completed 
     } 

     @Override 
     public void onCanceled() { 
      // do stuff when dialog is cancelled 
     } 
    }); 
    dialog.show(); 
+0

你也可以包括在構造函數中的聽衆,如果它適合你 – invertigo 2013-02-26 21:11:55