Android上的Datepicker出错,显示效果不佳

时间:2017-08-31 05:45:59

标签: android datepicker

我正在尝试打开一个对话框,用于在Android上选择片段中的日期。

public static 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) {
        // Do something with the date chosen by the user
    }
}

我有这个内部课程:

function blah(checkbox1, checkbox2, checkbox3, checkbox4, checkbox5, str) {
    if(checkbox1) {
        str += ':condition1';
    }
    if(checkbox2) {
        str += ':condition2';
    }
    if(checkbox3) {
        str += ':condition3';
    }
    if(checkbox4) {
        str += ':condition4';
    }
    if(checkbox5) {
        str += ':condition5';
    }
    return str;
}
console.log(blah(1, 0, 0, 0, 0, 'checkbox1'));
console.log(blah(0, 1, 0, 0, 0, 'checkbox2'));
console.log(blah(0, 0, 1, 0, 0, 'checkbox3'));
console.log(blah(1, 1, 0, 0, 0, 'checkbox1, checkbox2'));
console.log(blah(1, 0, 1, 0, 0, 'checkbox1, checkbox3'));
console.log(blah(1, 1, 1, 0, 0, 'checkbox1, checkbox2, checkbox3'));

问题是日期选择器显示效果不佳:like this pic

我使用xiaomi redmi note 4x来运行我的应用程序。 在模拟器中工作正常。

我的style.xml:https://pastebin.com/8dFXSa0A

2 个答案:

答案 0 :(得分:0)

您只需打开日期选择器,然后使用以下代码在edittext中选择和设置日期;

EditText from_date;
String date;

  final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    date = String.valueOf(dayOfMonth<10?("0"+dayOfMonth):(dayOfMonth)) +"-"+String.valueOf((monthOfYear+1)<10?("0"+(monthOfYear+1)):(monthOfYear+1))
                            +"-"+String.valueOf(year);
                    from_date.setText(date);
                }
            }, yy, mm, dd);
            datePicker.show();

答案 1 :(得分:0)

试试这个

public void openDatePicker() {
        //clearing error if any
        tvdate.setError(null);//it is textview
        final Calendar newCalendar = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(Getaddressforlaundry.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy", Locale.US);
                dateString = sdf.format(newDate.getTime());
                if (newCalendar.getTimeInMillis() < newDate.getTimeInMillis()) {
                    tvdate.setText(dateString);//it is textview
                    tvdate.setTag(null);//it is textview
                } else {
                    Toast.makeText(Getaddressforlaundry.this, getString(R.string.feature_date_selected), Toast.LENGTH_SHORT).show();
                    tvdate.setTag("false");//it is textview
                    tvdate.setError(getString(R.string.feature_date_selected));
                }

            }
        }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.getDatePicker().setMinDate(newCalendar.getTimeInMillis());
        datePickerDialog.show();


    }