如何在Fragment中使用showDialog()和DatePickerDialog()? [英] How to use showDialog() and DatePickerDialog() inside a Fragment?

查看:252
本文介绍了如何在Fragment中使用showDialog()和DatePickerDialog()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Inside an Activity, I am calling different fragments depending on which radiobutton is checked, different fragments are opened with different stuff that the user should input. For this one of the user must "Set Date" and "Set Time".

我正在使用此处的代码...
点击此处

I am using the code from here... click here.

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

It was working fine when I tried it in an Activity.. but when I put the code inside a fragment,

 showDialog(DATE_DIALOG_ID);

showDialog(TIME_DIALOG_ID);

有错误

对于新的View.OnclickListener(){

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--//





}

我在想..也许可以把这部分放在

I'm thinking.. maybe it is possible to put this part

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--//





    }

调用此片段的活动内。

如果没关系..我将如何在这个片段中调用该部分?

if that's okay.. how will i call that part in this fragment?

推荐答案

你需要得到一个句柄在Activity上调用showDialog()。尝试调用getActivity()。showDialog()

You need to get a handle on the Activity in order to call showDialog(). Try calling getActivity().showDialog()

这篇关于如何在Fragment中使用showDialog()和DatePickerDialog()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆