用户选择日期时更改标题

时间:2013-10-23 06:02:03

标签: android android-datepicker android-timepicker

当用户在日期选择器中进行更改时,我想更改标题(用户选择的日期显示)。我想使用此代码。我不会使用此代码听到

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();
}

1 个答案:

答案 0 :(得分: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);
                    }
                });