2016-03-06 54 views
0

DatePicker不與下面的代碼蹦出:Datepickerdialog採用質感設計

@Override 
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) { 
    String date = "You picked the following date: " + dayOfMonth + "/" + (monthOfYear + 1) + "/" + year; 
    TextView dateTextView = null; 
    dateTextView.setText(date); 

    Calendar now = Calendar.getInstance(); 
    final DatePickerDialog dpd = DatePickerDialog.newInstance(
      OneFragment.this, 
      now.get(Calendar.YEAR), 
      now.get(Calendar.MONTH), 
      now.get(Calendar.DAY_OF_MONTH) 
    ); 

    btntxt = (Button) v.findViewById(R.id.btndate); 
    btntxt.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final FragmentManager fm = getActivity().getFragmentManager(); 
      dpd.show(fm, "Datepickerdialog"); 
     } 
    }); 
} 

難道你們能幫我嗎?

回答

0

您的ActivityFragmentActivity延伸?

在這裏,你有DatePickerDialogTimePickerDialog一個很簡單的例子,這是非常相似的代碼與那些你需要修正你的代碼變化不大(也使用getSupportFragmentManager()越好):

public class MainActivity extends FragmentActivity implements OnDateSetListener, TimePickerDialog.OnTimeSetListener { 

    public static final String DATEPICKER_TAG = "datepicker"; 
    public static final String TIMEPICKER_TAG = "timepicker"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final Calendar calendar = Calendar.getInstance(); 

     final DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), isVibrate()); 
     final TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(this, calendar.get(Calendar.HOUR_OF_DAY) ,calendar.get(Calendar.MINUTE), false, false); 

     findViewById(R.id.dateButton).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       datePickerDialog.setVibrate(isVibrate()); 
       datePickerDialog.setYearRange(1985, 2028); 
       datePickerDialog.setCloseOnSingleTapDay(isCloseOnSingleTapDay()); 
       datePickerDialog.show(getSupportFragmentManager(), DATEPICKER_TAG); 
      } 
     }); 

     findViewById(R.id.timeButton).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       timePickerDialog.setVibrate(isVibrate()); 
       timePickerDialog.setCloseOnSingleTapMinute(isCloseOnSingleTapMinute()); 
       timePickerDialog.show(getSupportFragmentManager(), TIMEPICKER_TAG); 
      } 
     }); 

     if (savedInstanceState != null) { 
      DatePickerDialog dpd = (DatePickerDialog) getSupportFragmentManager().findFragmentByTag(DATEPICKER_TAG); 
      if (dpd != null) { 
       dpd.setOnDateSetListener(this); 
      } 

      TimePickerDialog tpd = (TimePickerDialog) getSupportFragmentManager().findFragmentByTag(TIMEPICKER_TAG); 
      if (tpd != null) { 
       tpd.setOnTimeSetListener(this); 
      } 
     } 
    } 

    private boolean isVibrate() { 
     return ((CheckBox) findViewById(R.id.checkBoxVibrate)).isChecked(); 
    } 

    private boolean isCloseOnSingleTapDay() { 
     return ((CheckBox) findViewById(R.id.checkBoxCloseOnSingleTapDay)).isChecked(); 
    } 

    private boolean isCloseOnSingleTapMinute() { 
     return ((CheckBox) findViewById(R.id.checkBoxCloseOnSingleTapMinute)).isChecked(); 
    } 

    @Override 
    public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) { 
     Toast.makeText(MainActivity.this, "new date:" + year + "-" + month + "-" + day, Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) { 
     Toast.makeText(MainActivity.this, "new time:" + hourOfDay + "-" + minute, Toast.LENGTH_LONG).show(); 
    } 
} 

來源:https://github.com/flavienlaurent/datetimepicker/blob/master/datetimepicker-sample/src/com/fourmob/datetimepicker/sample/MainActivity.java