如何在Fragment中使用showDialog()和DatePickerDialog()?

时间:2014-02-18 22:35:20

标签: java android datepicker fragment showdialog

在Activity中,我根据检查的radiobutton调用不同的片段,使用用户输入的不同内容打开不同的片段。 对于这个用户必须"设置日期"和"设定时间"。

我正在使用此处的代码......  click here

当我在一个Activity中尝试它时工作正常。 但是当我把代码放在一个片段中时,

 showDialog(DATE_DIALOG_ID);

showDialog(TIME_DIALOG_ID);

有错误

对于新类型View.OnclickListener(){

,未定义方法 showDialog(int)

return new DatePickerDialog(this, datetoperform, currentYear, currentMonth, currentDay);

有错误

构造函数DatePickerDialog(OrderTypeGeneralOrderFragment,DatePickerDialog.OnDateSetListener,int,int,int)未定义

return new TimePickerDialog(this, timeDate, hour, minute, false);

有错误

构造函数 **TimePickerDialog(OrderTypeGeneralOrderFragment, TimePickerDialog.OnTimeSetListener, int, int, boolean)**未定义

这是我的代码:

package info.androidhive.slidingmenu;

import java.util.Calendar;

import android.os.Bundle;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.TimePickerDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker; 
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class OrderTypeGeneralOrderFragment extends Fragment{

    private EditText subjectTextBox;


private int currentYear;
private int currentMonth;
private int currentDay;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
int hour;
int minute;
private Button btDate;
private Button btTime;
private TextView timeDisplay;
private TextView dateDisplay;



            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstancesState)
            {
                return inflater.inflate(R.layout.generalorderfragment, container, false);



            }



            @Override
            public void onActivityCreated(Bundle savedInstanceState){
                super.onActivityCreated(savedInstanceState);


                 Button   btDate  =(Button)  getView().findViewById(R.id.btnDate);
                 btDate.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                               showDialog(DATE_DIALOG_ID);
                        }
                    });
                    final Calendar c = Calendar.getInstance();
                    currentYear = c.get(Calendar.YEAR);
                    currentMonth = c.get(Calendar.MONTH);
                    currentDay = c.get(Calendar.DAY_OF_MONTH);


                Button btTime = (Button) getView().findViewById(R.id.btnTime);
                btTime.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                  showDialog(TIME_DIALOG_ID);
                            }
                        });
                        final Calendar d = Calendar.getInstance();
                        hour = d.get(Calendar.HOUR_OF_DAY);
                        minute = d.get(Calendar.MINUTE);


                TextView timeDisplay = (TextView) getView().findViewById(R.id.txtTime);
                TextView dateDisplay = (TextView) getView().findViewById(R.id.txtDate);
                EditText subjectTextBox = (EditText) getView().findViewById(R.id.subjectTextBoxId);





            }













          //--DATE PICKER--//

             // Use the current date as the default date in the picker


            protected Dialog onCreateDialog4(int id) {


                    switch (id) {
                    case DATE_DIALOG_ID:
                        return new DatePickerDialog(this, datetoperform, currentYear, currentMonth, currentDay);
                    case TIME_DIALOG_ID:
                        return new TimePickerDialog(this, timeDate, hour, minute, false);
                    }
                    return null;
                }

               private DatePickerDialog.OnDateSetListener datetoperform = new DatePickerDialog.OnDateSetListener() {

                   public void onDateSet(DatePicker view, int year, int month, int day) {
                     //   if (c1.isChecked()) {

                            dateDisplay.setText("Order will be performed on: "
                                    + (month + 1) + "-" + day + "-" + year +  ".");
                      //  }
                    }



                };


                private TimePickerDialog.OnTimeSetListener timeDate = new TimePickerDialog.OnTimeSetListener() {

                    public void onTimeSet(TimePicker view, int hours, int minutes) {
                        timeDisplay.setText("on " + hours + ":"
                                + minutes + ".");
                    }
                };


              /*  public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.activity_main, menu);
                    return true;
                }*/

                //--//DATE PICKER--//





}

我在想......或许有可能把这部分放在

protected Dialog onCreateDialog4(int id) {


                        switch (id) {
                        case DATE_DIALOG_ID:
                            return new DatePickerDialog(this, datetoperform, currentYear, currentMonth, currentDay);
                        case TIME_DIALOG_ID:
                            return new TimePickerDialog(this, timeDate, hour, minute, false);
                        }
                        return null;
                    }

                   private DatePickerDialog.OnDateSetListener datetoperform = new DatePickerDialog.OnDateSetListener() {

                       public void onDateSet(DatePicker view, int year, int month, int day) {
                         //   if (c1.isChecked()) {

                                dateDisplay.setText("Order will be performed on: "
                                        + (month + 1) + "-" + day + "-" + year +  ".");
                          //  }
                        }



                    };


                    private TimePickerDialog.OnTimeSetListener timeDate = new TimePickerDialog.OnTimeSetListener() {

                        public void onTimeSet(TimePicker view, int hours, int minutes) {
                            timeDisplay.setText("on " + hours + ":"
                                    + minutes + ".");
                        }
                    };


                  /*  public boolean onCreateOptionsMenu(Menu menu) {
                        // Inflate the menu; this adds items to the action bar if it is present.
                        getMenuInflater().inflate(R.menu.activity_main, menu);
                        return true;
                    }*/

                    //--//DATE PICKER--//





    }

调用此片段的活动内部。

如果可以的话......我将如何在这个片段中调用该部分?

2 个答案:

答案 0 :(得分:3)

您需要获取Activity的句柄才能调用showDialog()。尝试调用getActivity()。showDialog()

答案 1 :(得分:0)

谢谢..对话现在出现了:D

TextView timeDisplay =(TextView)getView()。findViewById(R.id.txtTime); TextView dateDisplay =(TextView)getView()。findViewById(R.id.txtDate);

不会显示所选的日期和时间。

我有我的活动:

protected Dialog onCreateDialog(int id) {
        switch (id) {

         case DATE_DIALOG_ID:


            OnDateSetListener datetoperform = null;// i had to initialize this because datetoperform is in the fragment 



            return new DatePickerDialog(this, datetoperform, currentYear,currentMonth, currentDay);


            case TIME_DIALOG_ID2:

            OnTimeSetListener timeDate = null;// i had to initialize this because timeDate is in the fragment 
            return new TimePickerDialog(this, timeDate, hour, minute, false);

        case DIALOG_CHANGE_BACKGROUND:
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setSingleChoiceItems(mBgNames, mChangeBGItem, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {                  
                    mChangeBGItem = which;
                    Bitmap bm = null;
                    switch(which) {
                    case 0:
                        bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.letter_bg_grass);
                        break;
                    case 1:
                        bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.letter_bg_present);
                        break;
                    case 2:
                        bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.letter_bg);
                        break;
                    case 3:
                        mSCanvas.setBGColor(0x00000000);
                        break;




                    }   
                    if(bm != null) {
                        mSCanvas.setBGImage(bm);
                    }
                    removeDialog(DIALOG_CHANGE_BACKGROUND);
                }
            });
            return builder.create();
        }
        return super.onCreateDialog(id);

OnDateSetListener datetoperform = null;

我必须初始化这个因为datetoperform在片段中

OnDateSetListener timeDate = null;

我必须初始化这个因为timeDate在片段中

在Fragement中:

 public DatePickerDialog.OnDateSetListener datetoperform = new DatePickerDialog.OnDateSetListener() {

                   public void onDateSet(DatePicker view, int year, int month, int day) {
                     //   if (c1.isChecked()) {




                       dateDisplay.setText("Order will be performed on: "
                                    + (month) + "-" + day + "-" + year +  ".");
                      //  }
                    }



                };


         public TimePickerDialog.OnTimeSetListener timeDate = new TimePickerDialog.OnTimeSetListener() {

             public void onTimeSet(TimePicker view, int hours, int minutes) {
                 timeDisplay.setText("on " + hours + ":"
                         + minutes + ".");
             }
         };