在片段内显示dialogfragment

时间:2014-06-27 15:23:44

标签: android android-activity android-fragments android-dialogfragment

我想显示一个在片段中实现datepickerdialog的dialogfragment! 我有一个按钮,其监听器显示datepickerdialog。 该按钮在片段布局中定义为:

<Button
        android:id="@+id/buttonSelectDate"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content"
    android:onClick="showDatePickerDialog" />

片段类的代码如下(我的问题在showDatePickerDialog函数中):

public  class SchedulerChooserFragment extends Fragment {

    private int year;
    private int month;
    private int day;

    TextView dateText;   
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        LayoutInflater lf = getActivity().getLayoutInflater(); 
        rootView = lf.inflate(R.layout.fragment_scheduler_chooser, container,false);

        return rootView;
    }



    // display current date
    public void showCurrentDateOnView() {

        dateText = (TextView) rootView.findViewById (R.id.textDate);

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        System.out.println("year" + year);
        // set current date into textview
        dateText.setText(month + 1 +"-"+day+"-"+year);

        // set current date into datepicker


    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment(dateText);
        newFragment.show(getSupportFragmentManager(), "datePicker");            
    }

}

但是,片段内不存在getSupportFragmentManager。 我尝试过getActivity()。getSupportFragmentManager但应用程序崩溃了。 另外,我尝试过getChildFragmentManager,应用程序崩溃了。

datePickerFragment类如下:

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import android.widget.TextView;

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    TextView txtDate;


     public DatePickerFragment(TextView txtDate) {
        super();
        this.txtDate = txtDate;
    }

    @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            /*txtDate.setText(year+" " +month +" "+ date);*/
        }



}

我是android新手!对不起,如果我的问题很明显。

由于

4 个答案:

答案 0 :(得分:0)

在SchedulerChooserFragment中,fragment和FragmentManager的导入必须都来自android.support.v4.app

所以,

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

答案 1 :(得分:0)

DialogFragment newFragment = new DatePickerFragment(dateText);

dateText是TextView - 您无法将TextView设置为Dialog;)

编辑:显示您的错误日志;)

答案 2 :(得分:0)

我发现了错误。 Log输出抛出java.lang.IllegalStateException:关于找不到定义为android的showDatePickerDialog:onClick =“showTimePickerDialog”  按钮。

但是,不可能在片段内定义像这样的onclick方法。 所以我将SchedulerChooserFragment更改为:

public  class SchedulerChooserFragment extends Fragment   implements OnClickListener{

        View rootView;
        TextView textDisplayDate;
        private int year;
        private int month;
        private int day;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.fragment_scheduler_chooser, container,false);
            showCurrentDateOnView();
            Button b = (Button) rootView.findViewById(R.id.buttonSelectDate);
            b.setOnClickListener(this);
            return rootView;
        }

     // display current date
            public void showCurrentDateOnView() {

                textDisplayDate = (TextView) rootView.findViewById(R.id.textDate);


                final Calendar c = Calendar.getInstance();
                year = c.get(Calendar.YEAR);
                month = c.get(Calendar.MONTH);
                day = c.get(Calendar.DAY_OF_MONTH);

                // set current date into textview
                textDisplayDate.setText(year+" "+month+" "+day);


            }

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                case R.id.buttonSelectDate:

                     DialogFragment newFragment = new DatePickerFragment(textDisplayDate);
                    newFragment.show(getChildFragmentManager(), "datePicker");

                    break;
                }

            }


    }

答案 3 :(得分:0)

DialogFragment dFragment = new DatePickerFragment();
                dFragment.show(getFragmentManager(), "Date Picker");

Fragment中的对话片段

使用此

导入android.support.v4.app.DialogFragment;

取代

android.app.DialogFragment;