2016-09-25 9 views
4

からダイアログを呼び出し、私が代わりに各アクティビティにダイアログの繰り返しコードを書くのは、私だけでダイアログを表示するために、すべてのメソッドが含まれている1つのクラスを作成し、私は小さなコードスニペットに別のクラス

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(); 
    } 

} 
を与えている、アンドロイドに初心者です

は、私は、コードのダイアログが表示されているが、ボタンはありませんが、これが理由DialogInterace.onClickListenerである実行すると?私は知りたい

//dialogObj is instance of the above class 

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

を呼び出すアム、このように行うには、この良いアイデアです?それが正しいとすれば、正しい方法です。私を助けてください。

ありがとうございました。その後、別のクラスはBaseActivityを拡張し、ちょうど私がどのように私の方法を共有したい

showDialog(DIALOG_X); 

答えて

4

を呼び出す

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; 
} 
} 

それを呼び出して、基本クラスにすべてのダイアログを定義するための

+0

お返事ありがとうございます。 – Manjunath

+0

素晴らしい、あなたを助けるために幸せ:) – Saveen

0

ベストな方法私は使用しています。あなたはクラスを好きな場所に呼び出すことができます。

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(); 
} 

ちょうどあなたがダイアログを使用したいあなたの活動で、このインタフェースを実装したクラスのオブジェクトの助けを借りて、あなたは、このようなダイアログを使用することができ、このダイアログのための1つのインタフェースを作成しています、

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

あなたacitivtyでこれらの2つのメソッドをオーバーライドすることができ、

@Override 
public void onPositiveButtonClicked() { 

} 

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

} 

お手数ですが、これがお手伝いします。