2014-04-03 25 views
0

我是自定義對話框中的每個活動寫自定義對話框的代碼。如何編寫通用的方法傳遞兩個參數的上下文和消息字符串?請給我任何建議。如何使用java爲整個android應用程序創建自定義對話框常用方法?

final Dialog dialog = new Dialog(Activity.this,R.style.DialogTheme); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.notify_received_activity); 
// set the custom dialog components - text and button 
TextView text = (TextView) dialog.findViewById(R.id.txtmsg); 
text.setText("Tracking Number : " + mTrackingNR); 
Button dialogButton = (Button) dialog.findViewById(R.id.btncancel); 
// if button is clicked, close the custom dialog 
dialogButton.setOnClickListener(new OnClickListener() { 
public void onClick(View v) {dialog.dismiss();} }); 
dialog.show(); 

回答

1
/** 
     * Display Dialog 
     **/ 
     public static void showDialog(final Context context, String message) { 
      Dialog dialog = new Dialog(context,R.style.DialogTheme); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.notify_received_activity); 
    // set the custom dialog components - text and button 
    TextView text = (TextView) dialog.findViewById(R.id.txtmsg); 
    text.setText(message); 
    Button dialogButton = (Button) dialog.findViewById(R.id.btncancel); 
    // if button is clicked, close the custom dialog 
    dialogButton.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) {dialog.dismiss();} }); 
    dialog.show(); 
     } 

,只是下面的任何地方撥打:

showDialog(ActivityName.this,"message"); 
0

這裏是你想做的事:

public static void showAlert(Context context, String message) 
     { 
      final Dialog dialog = new Dialog(context); 
      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      dialog.setContentView(R.layout.custom_dialog); 
      TextView messageTextView = (TextView) dialog.findViewById(R.id.customDialogMessageTextView); 
      messageTextView.setText(message); 
      Button okButton = (Button) dialog.findViewById(R.id.customDialogOkButton); 
      okButton.setOnClickListener(new OnClickListener() 
       { 
        @Override 
        public void onClick(View view) 
         { 
          dialog.dismiss(); 
         } 
       }); 
      dialog.show(); 
     } 

棘手的部分是,如果你想要的操作dialog.dismiss()每用戶點擊確定按鈕。

根據對話框的不同,您需要自定義實現,您必須單獨爲該對話框編寫代碼。

可以通過從您打電話的地方傳入context來調用此方法。

可以在普通的java類中聲明,不必是一個活動。例如。 Utility.showAlert(activityContext, "your message or variable");

0

如何在Android documentation指定,你可以傳遞參數以下列方式對話框......

public static class MyDialogFragment extends DialogFragment { 
    int mNum; 

    /** 
    * Create a new instance of MyDialogFragment, providing its arguments 
    */ 
    static MyDialogFragment newInstance(int num, /*other parameters*/) { 
     MyDialogFragment f = new MyDialogFragment(); 

     Bundle args = new Bundle(); 
     args.putInt("num", num); 
    //put all parameters on the Bundle 
     f.setArguments(args); 

     return f; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mNum = getArguments().getInt("num"); 
    //get the arguments 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    //create the view 

     return v; 
    } 
} 
相關問題