2016-09-25 85 views
4

我初學者到Android調用對話,而不是在每個活動重複編寫代碼的對話,我剛剛創建,其包含了所有的方法來顯示對話框一個班,我已經給了一小段代碼從另一個類

public class Dialogues extends Activity implements DialogueMethods { 


    public void showAlertDialog(Context context, String title, String message) { 

     AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
     alertDialog.setTitle(title); 
     alertDialog.setMessage(message); 
     alertDialog.show(); 
} 

    //this method am calling 

    public void showAlertDialog(Context context, String title, String message, String ButtonText, boolean cancel) { 

     AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
     alertDialog.setTitle(title); 
     alertDialog.setMessage(message); 

     if(cancel) { 

      alertDialog.setNegativeButton(ButtonText, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.cancel(); 
       finish(); 
      } 
     }); 
     } 

     alertDialog.show(); 
    } 

} 

我打電話

//dialogObj is instance of the above class 

dialogObj.showAlertDialog(MyActivity.this, "Error", "Not Connected to Internet", "Exit", true); 

當我運行代碼對話框是可見的,但按鈕沒有,這是因爲DialogInterace.onClickListener的?我只是想知道這是好主意,做這樣的?如果是的話,那麼正確的做法是什麼。請幫幫我。

謝謝。

回答

4

正是我要我的方式分享給大家如何我正在使用。你可以讓課堂喜歡,然後打電話到你想要的地方。

public class DialogsUtil { 

private Context mContext; 

public DialogsUtil(Context context) { 
    this.mContext = context; 
} 

/** 
* Return an alert dialog 
* 
* @param message message for the alert dialog 
* @param listener listener to trigger selection methods 
*/ 
public void openAlertDialog(Context context, String message, String positiveBtnText, String negativeBtnText, 
          final OnDialogButtonClickListener listener) { 

    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setPositiveButton(positiveBtnText, (dialog, which) -> { 
     dialog.dismiss(); 
     listener.onPositiveButtonClicked(); 

    }); 

    builder.setNegativeButton(negativeBtnText, (dialog, which) -> { 
     dialog.dismiss(); 
     listener.onNegativeButtonClicked(); 

    }); 
    builder.setTitle(context.getResources().getString(R.string.app_name)); 
    builder.setMessage(message); 
    builder.setIcon(android.R.drawable.ic_dialog_alert); 
    builder.setCancelable(false); 
    builder.create().show(); 
} 

/** 
* return a dialog object 
* @return 
*/ 
public Dialog openDialog(@LayoutRes int layoutId) { 
    Dialog dialog = new Dialog(mContext); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); 
    dialog.setContentView(layoutId); 
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    dialog.getWindow().setGravity(Gravity.BOTTOM); 
    dialog.setCancelable(true); 
    dialog.setCanceledOnTouchOutside(false); 
    return dialog; 
} 
} 

,我已經創建了一個接口,這個對話框中,

public interface OnDialogButtonClickListener { 

void onPositiveButtonClicked(); 

void onNegativeButtonClicked(); 
} 

只是實現您的活動要使用對話框並用類對象的幫助,您可以使用對話框這樣這個接口,

mDialogsUtil.openAlertDialog(YourActivity.this, "text message", "positive button msg", "Negative button msg", this); 

您可以覆蓋在你的acitivty這兩種方法,

@Override 
public void onPositiveButtonClicked() { 

} 

//user clicked cancel.Close the application 
@Override 
public void onNegativeButtonClicked() { 

} 

謝謝希望這會幫助你。

+0

感謝您的清理。 – Manjunath

+0

太棒了,高興地幫助你:) – Saveen

0
來定義一個基類所有的對話框

的最佳方式,然後調用它

Class BaseActivity extends Activity{ 

int DIALOG_X = 1; 
int DIALOG_Y = 2; 
int DIALOG_Z = 3; 
// More Dialog identifiers 

ProgressDialog progressDialog; 
AlertDialog alertDialog; 
//More dialog objects if you need 

protected Dialog onCreateDialog(int id) { 
    Dialog dialog; 
    switch(id) { 
    case DIALOG_X: 
     // do the work to define the X Dialog 
     break; 
    case DIALOG_Y: 
     // do the work to define the Y Dialog 
     break; 
    default: 
     dialog = null; 
    } 
    return dialog; 
} 
} 

然後另一個類擴展BaseActivity並調用

showDialog(DIALOG_X);