2

我正在使用自定义 DatePicker,但在 Month 字段中遇到问题。当我在 DatePicker 中选择 May 月份时,它看起来不太好。并且仅在 320 X 480 Xpheria 设备中显示。

我得到了如图所示的视图 你可以看到五月

其余所有值均正确显示。

我正在使用下面的 xml 代码。

<DatePicker
        android:id="@+id/dpResult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvDate"
        android:layout_marginTop="5dp" />






    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_date_popup);

        setCurrentDateOnView();
        //addListenerOnButton();
        _setButton = (Button)findViewById(R.id.set_button);
        _setButton.setOnClickListener(this);
        _cancelButton = (Button)findViewById(R.id.cancel_button);
        _cancelButton.setOnClickListener(this);

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

        //showDialog(DATE_DIALOG_ID);
        _datePicker = (DatePicker)findViewById(R.id.dpResult);
        System.out.print(_datePicker.getDayOfMonth());
        try{
            java.lang.reflect.Field[] f = _datePicker.getClass().getDeclaredFields();
            for (java.lang.reflect.Field field : f) {
                if (field.getName().equals("mDaySpinner") ||field.getName().equals("mDayPicker")) {
                    field.setAccessible(true);
                    Object yearPicker = new Object();
                    yearPicker = field.get(_datePicker);
                    ((View) yearPicker).setVisibility(View.GONE);
                }
            }

        }catch(Exception e){
            e.printStackTrace();
        }

    }


    // display current date
    public void setCurrentDateOnView() {

        tvDisplayDate = (TextView) findViewById(R.id.tvDate);
        dpResult = (DatePicker) findViewById(R.id.dpResult);

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

        // set current date into textview
        // Month is 0 based, just add 1
        /*  tvDisplayDate.setText(new StringBuilder()
        .append(month + 1).append("-").append(day).append("-")
        .append(year).append(" "));*/

        // set current date into datepicker
        if(Constant.SELECTED_YEAR!=0 && Constant.SELECTED_MONTH_INTEGER!=0)
            dpResult.init(Constant.SELECTED_YEAR, Constant.SELECTED_MONTH_INTEGER, day, null);
        else
            dpResult.init(year, month, day, null);


    }


    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            try{
                java.lang.reflect.Field[] f = datePickerListener.getClass().getFields();
                for (java.lang.reflect.Field field : f) {
                    if (field.getName().equals("mDayPicker")) {
                        field.setAccessible(true);
                        Object yearPicker = new Object();
                        yearPicker = field.get(datePickerListener);
                        ((View) yearPicker).setVisibility(View.GONE);
                    }
                }

            }catch(Exception e){
                e.printStackTrace();
            }
            // set date picker as current date
            return new DatePickerDialog(this, datePickerListener, year, month, day);
        }
        return null;
    }


    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            tvDisplayDate.setText(new StringBuilder().append(month + 1).append("-").append(day).append("-").append(year).append(" "));

            // set current date into datepicker
            if(Constant.SELECTED_YEAR!=0 && Constant.SELECTED_MONTH_INTEGER!=0)
                dpResult.init(Constant.SELECTED_YEAR, Constant.SELECTED_MONTH_INTEGER, day, null);
            else
                dpResult.init(year, month, day, null);
        }
    };

    @Override
    public void onClick(View v) {
        if(v==_setButton){
            //CALL THIS STATEMENT TO INITIALIZE THE VALUE
            _datePicker.getMonth();

            int month = _datePicker.getMonth();
            //Constant.SELECTED_MONTH_INTEGER=month;
            String monthString = monthValue(month);
            //Constant.SELECTED_MONTH= monthString;
            int yearString = _datePicker.getYear();
            /*Intent intent = new Intent();
            intent.putExtra("SELECTED_MONTH_INTEGER", month);
            intent.putExtra("SELECTED_MONTH", monthString);
            intent.putExtra("SELECTED_YEAR", yearString);
            setResult(RESULT_CODE, intent);
            MyAndroidAppActivity.this.finish();*/
            //Constant.SELECTED_YEAR = yearString;
            if(month==currentMonth && yearString==currentYear){
                Intent intent = new Intent();
                intent.putExtra("SELECTED_MONTH_INTEGER", month);
                intent.putExtra("SELECTED_MONTH", monthString);
                intent.putExtra("SELECTED_YEAR", yearString);
                setResult(RESULT_CODE, intent);
                MyAndroidAppActivity.this.finish();
            }else if(month>currentMonth){
                if(yearString<currentYear){
                    Constant.showToast(MyAndroidAppActivity.this, "Invalid selection.");
                }else if(yearString>=currentYear){
                    Intent intent = new Intent();
                    intent.putExtra("SELECTED_MONTH_INTEGER", month);
                    intent.putExtra("SELECTED_MONTH", monthString);
                    intent.putExtra("SELECTED_YEAR", yearString);
                    setResult(RESULT_CODE, intent);
                    MyAndroidAppActivity.this.finish();
                }
            }else if(month<currentMonth){
                if(yearString<currentYear){
                    Constant.showToast(MyAndroidAppActivity.this, "Invalid selection.");
                }else if(yearString>currentYear){
                    Intent intent = new Intent();
                    intent.putExtra("SELECTED_MONTH_INTEGER", month);
                    intent.putExtra("SELECTED_MONTH", monthString);
                    intent.putExtra("SELECTED_YEAR", yearString);
                    setResult(RESULT_CODE, intent);
                    MyAndroidAppActivity.this.finish();
                }else{
                    Constant.showToast(MyAndroidAppActivity.this, "Invalid selection.");
                }
            }else if(month==currentMonth){
                if(yearString<currentYear){
                    Constant.showToast(MyAndroidAppActivity.this, "Invalid selection.");
                }else if(yearString>currentYear){
                    Intent intent = new Intent();
                    intent.putExtra("SELECTED_MONTH_INTEGER", month);
                    intent.putExtra("SELECTED_MONTH", monthString);
                    intent.putExtra("SELECTED_YEAR", yearString);
                    setResult(RESULT_CODE, intent);
                    MyAndroidAppActivity.this.finish();
                }else{
                    Constant.showToast(MyAndroidAppActivity.this, "Invalid selection.");
                }
            }else{
                Constant.showToast(MyAndroidAppActivity.this, "Invalid selection.");
            }
        }else if(v==_cancelButton){
            MyAndroidAppActivity.this.finish();
        }

    }

    /**
     * monthValue
     * @param intMonth
     * @return
     */
    private String monthValue(int intMonth) {
        String month=null;

        switch (intMonth) {
        case 0:
            month = "Jan";
            break;
        case 1:
            month = "Feb";
            break;
        case 2:
            month = "Mar";
            break;
        case 3:
            month = "Apr";
            break;
        case 4:
            month = "May";
            break;
        case 5:
            month = "Jun";
            break;
        case 6:
            month = "Jul";
            break;
        case 7:
            month = "Aug";
            break;
        case 8:
            month = "Sep";
            break;
        case 9:
            month = "Oct";
            break;
        case 10:
            month = "Nov";
            break;
        case 11:
            month = "Dec";
            break;
        default:
            break;
        }

        return month;
    }

}
4

0 回答 0