无法获取DatePicker选择当前日期(ANDROID),保持上个月启用

时间:2017-04-05 05:34:36

标签: android datepicker

我设法得到日期选择器只有前一个月可见,但我的光标默认选择上个月的日期,有人可以帮助我解决这个问题....我希望上个月可见光标选择当前日期在开放。

请提前帮助,谢谢你......

这是我的代码

public class SearchFragment extends Fragment {


    ListView lstv22;
    ArrayList<AshTable2> array222;

    EditText inputSearch1;
    CustomListViewAdapter2 adapter2;

    int day1, month1, year1;

    private int mYearIni, mMonthIni, mDayIni, sYearIni, sMonthIni, sDayIni, mYearIni1, mMonthIni1, mDayIni1, sYearIni2, sMonthIni2, sDayIni2, mYearIni3, mMonthIni3, mDayIni3;

    Calendar C = Calendar.getInstance();

    TextView noSearch;


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


    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_fragment,
                container, false);


//        sMonthIni = C.get(Calendar.MONTH);
//        sDayIni = C.get(Calendar.DAY_OF_MONTH);
//        sYearIni = C.get(Calendar.YEAR);


        getActivity().setTitle("Search");
        array222 = new ArrayList<>();

        lstv22 = (ListView) view.findViewById(R.id.lst_vw2);

        inputSearch1 = (EditText) view.findViewById(R.id.inputSearch1);

        noSearch = (TextView) view.findViewById(R.id.noSearch);


        inputSearch1.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable arg) {
                // TODO Auto-generated method stub

                String text = inputSearch1.getText().toString().toLowerCase(Locale.getDefault());
                adapter2.filter(text);
                listCheck();


            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {

            }

        });

        adapter2 = new CustomListViewAdapter2(getActivity(), R.layout.home_list_view, array222);


        lstv22.setAdapter(adapter2);


        inputSearch1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(inputSearch1.getWindowToken(), 0);

//                final Calendar cal = Calendar.getInstance();
//                mYearIni=cal.get(Calendar.YEAR);
//                mMonthIni=cal.get(Calendar.MONTH);
//                mDayIni=cal.get(Calendar.DAY_OF_MONTH);

                showDispatchCalendar();

            }
        });


        return view;


    }



    private void showDispatchCalendar() {
        Calendar c1 = Calendar.getInstance();
        DatePickerDialog da = new DatePickerDialog(getActivity(), mDateSetListener, mYearIni, mMonthIni, mDayIni);
        c1.add(Calendar.MONTH, -1);
//        long currentTime = new Date(System.currentTimeMillis()).getTime();
//        Date newDate = new Date(currentTime - 29 * 24 * 3600 * 1000);

        da.getDatePicker().setMinDate(new Date(System.currentTimeMillis()).getTime());
        da.show();

//        long newDate = System.currentTimeMillis();
//        da.getDatePicker().setMinDate(newDate);
//        da.getDatePicker().setMinDate(newDate);

        da.show();

    }


    private void colocar_fecha() {

        day1 = mDayIni;
        month1 = mMonthIni + 1;
        year1 = mYearIni;
        inputSearch1.setText(day1 + "-" + month1 + "-" + year1 + " ");
    }


    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    mYearIni = year;
                    mMonthIni = monthOfYear;
                    mDayIni = dayOfMonth;

                    colocar_fecha();
                }
            };




    public void listCheck() {

        if (adapter2.getCount() != 0) {
            noSearch.setText(" ");
        } else {

            noSearch.setText(" No Search Result Found ");

        }

    }



}

5 个答案:

答案 0 :(得分:0)

showDispatchCalendar()方法中,请使用:

Calendar nowCalendar = Calendar.getInstance();
        DatePickerDialog = DatePickerDialog.newInstance(
                CreateEventActivity.this,
                nowCalendar.get(Calendar.YEAR),
                nowCalendar.get(Calendar.MONTH),
                nowCalendar.get(Calendar.DAY_OF_MONTH)
        );

答案 1 :(得分:0)

试试这个

long currentTime = new Date(System.currentTimeMillis()).getTime();
Date newDate = new Date(currentTime - 29 * 24 * 3600 * 1000); //Subtract 30 days

在您的代码中设置此日期..

da.getDatePicker().setMinDate(newDate.getTime());
da.show();

希望它会对你有帮助,Check&amp;让我知道

被修改

Calendar c = Calendar.getInstance();
DatePickerDialog dd = new DatePickerDialog(activity,
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    try {
                        // Do your stuff
                    } catch (Exception ex) {

                    }
                }
            }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
dd.show();

答案 2 :(得分:0)

在致电showDispatchCalendar()之前添加此内容...

final Calendar cal = Calendar.getInstance();
            mYearIni=cal.get(Calendar.YEAR);
            mMonthIni=cal.get(Calendar.MONTH);
            mDayIni=cal.get(Calendar.DAY_OF_MONTH);

答案 3 :(得分:0)

首先创建DatePicker类,如下所示:

public class DatePickerFragment extends DialogFragment {
DatePickerDialog.OnDateSetListener ondateSet;
private int year, month, day;

public DatePickerFragment() {}

public void setCallBack(DatePickerDialog.OnDateSetListener ondate) {
    ondateSet = ondate;
}

@SuppressLint("NewApi")
@Override
public void setArguments(Bundle args) {
    super.setArguments(args);
    year = args.getInt("year");
    month = args.getInt("month");
    day = args.getInt("day");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, ondateSet, year, month, day);
}

}

然后在任何类中使用它:

private void showDatePicker() {
    DatePickerFragment date = new DatePickerFragment();
    /**
     * Set Up Current Date Into dialog
     */
    Calendar calender = Calendar.getInstance();
    Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date.setArguments(args);
    /**
     * Set Call back to capture selected date
     */
    date.setCallBack(ondate1);

    date.show(getActivity().getFragmentManager(), "Date Picker");
}

public String checkDigit(int number) {
    return number <= 9 ? "0" + number : String.valueOf(number);
}

DatePickerDialog.OnDateSetListener ondate = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {

        etDob.setText(String.valueOf(checkDigit(dayOfMonth)) + "/" + String.valueOf(checkDigit(monthOfYear + 1))
                + "/" + String.valueOf(year));
    }
};

这样您将始终获得当前日期。

最好的运气!

答案 4 :(得分:0)

试试这个:

 // 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) + 1;
            int day = c.get(Calendar.DAY_OF_MONTH);

            Log.e("DialogMonth", ""+month);