0

当用户在日期选择器中进行更改时,我想更改标题(用户选择的要显示的日期)。任何想法我正在使用此代码。ondatechange 不工作听到

public void showDatePicker(){
    LayoutInflater inflat=(LayoutInflater) getLayoutInflater();
    final AlertDialog.Builder pick=new AlertDialog.Builder(this);
    View customView=inflat.inflate(R.layout.pickdate, null);

    final DatePicker datePicker = 
            (DatePicker) customView.findViewById(R.id.datePicker1);
    TimePicker timePicker=(TimePicker) customView.findViewById(R.id.timePicker1);
    timePicker.setBackgroundResource(R.drawable.ic_launcher);
    //datePicker.setMinDate(minDate.getTimeInMillis());
    String title=String.valueOf(datePicker.getDayOfMonth());
    pick.setView(customView);
    pick.setTitle(title);
    Calendar today=Calendar.getInstance();       
     pick.setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            //Toast.makeText(getApplicationContext(), String.valueOf(datePicker.getYear()), Toast.LENGTH_SHORT).show();

        }
    });
     pick.setNegativeButton(android.R.string.cancel,new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {


            }
        });                   
    pick.show();
}
4

1 回答 1

0

试试这个。它在更改日期时对我有用。

Calendar thisDay;   
thisDay = Calendar.getInstance();   


datePicker.init(thisDay.get(Calendar.YEAR),
                thisDay.get(Calendar.MONTH),
                thisDay.get(Calendar.DAY_OF_MONTH),
                new OnDateChangedListener() {
                    @Override
                    public void onDateChanged(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        String myDate = String.valueOf(dayOfMonth);
                        String myMonth =String.valueOf(monthOfYear);

                        if (myDate.length() == 1) {
                            myDate = "0" + dayOfMonth;
                        }
                        if (myMonth.length() == 1 && monthOfYear < 9) {
                            myMonth = "0" + (monthOfYear + 1);
                        } else {
                            myMonth = String.valueOf((monthOfYear + 1));
                        }
                        textview.setText(year + "-" + myMonth + "-" + myDate);
                    }
                });
于 2013-10-23T06:14:40.440 回答