showDialog未显示Android

时间:2017-03-23 07:31:58

标签: java android date dialog

Activity内,我正在调用不同的片段,我希望用户能够通过点击显示当前日期textView的{​​{1}}来更改日期。不想被人展示。

showDialog

Android Studio告诉我dateView.setOnClickListener(new View.OnClickListener() { @SuppressWarnings("deprecation") public void onClick(View v) { getActivity().showDialog(999); } }); 从未使用过。

CreateDialog
  

完整代码:

protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    if (id == 999) {
        return new DatePickerDialog(getContext(),
                myDateListener, year, month, day);
    }
    return null;
}

MainActivity

public class Fragment5 extends android.support.v4.app.Fragment {
    private DatePicker datePicker;
    private Calendar calendar;
    private TextView dateView;
    TimePicker timePicker2;
    private int year, month, day;
    public Fragment5() {
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_fragment5, container, false);
        dateView = (TextView) rootView.findViewById(R.id.textView3);
        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);

        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        showDate(year, month+1, day);

        dateView.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            public void onClick(View v) {

                getActivity().showDialog(999);

            }
        });

        return rootView;
    }

    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        if (id == 999) {
            return new DatePickerDialog(getContext(),
                    myDateListener, year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener myDateListener = new
            DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker arg0,
                                      int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub
                    // arg1 = year
                    // arg2 = month
                    // arg3 = day
                    showDate(arg1, arg2+1, arg3);
                }
            };

    private void showDate(int year, int month, int day) {
        dateView.setText(new StringBuilder().append(day).append("/")
                .append(month).append("/").append(year));
    }
}

}

3 个答案:

答案 0 :(得分:1)

从那以后,

  

回复创建由活动管理(保存和恢复)的dialogs

Activity而不是Fragment

中实施此方法
@Override
protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        if (id == 999) {
            return new DatePickerDialog(getContext(),
                    myDateListener, year, month, day);
        }
        return null;
    }

像这样调用Fragment;

private void showDate(int year, int month, int day) {

StringBuilder abbes = new StringBuilder().append(day).append("/")
            .append(month).append("/").append(year);
    if(fragment instanceof Fragment4){
       ((Fragemnt4)fragment).dateView.setText(""+abbes);
    }

}

将dateView设为公开

答案 1 :(得分:0)

不推荐使用showDialog()。替换此代码

 public class Fragment5 extends android.support.v4.app.Fragment {
private DatePicker datePicker;
private Calendar calendar;
private TextView dateView;
TimePicker timePicker2;
private int year, month, day;
DialogFragment dateFragment;
public Fragment5() {
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_fragment5, container, false);
    dateView = (TextView) rootView.findViewById(R.id.textView3);
    calendar = Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);

    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);
    showDate(year, month+1, day);

    dateView.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {

             dateFragment = new DatePickerFragment();
    dateFragment.show(getFragmentManager(), "datePicker");

        }
    });

    return rootView;
}





private void showDate(int year, int month, int day) {
    dateView.setText(new StringBuilder().append(day).append("/")
            .append(month).append("/").append(year));
}

class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @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) {
      showDate(year,month,day);
    }
} }

答案 2 :(得分:0)

showDialog (int id)方法的作用是显示由activity管理的对话框。第一次为给定的id调用时,对onCreateDialog(int, Bundle)的调用将使用相同的id。从此后,将自动保存并恢复对话框。

如@Burhanuddin Rashid所述,您必须在onCreateDialog中编写Activity方法,但这可能会导致ActivityFragment之间数据转换的复杂性。

进一步

void showDialog (int id)

此方法在API级别13中已弃用。
使用新的DialogFragment课程代替FragmentManager;这也可以通过Android兼容包在旧版平台上使用。

PS,
我建议您使用DialogFragment

有关详细信息,请查看此official documentation