Android-日期选择器-Obatin值
我在活动中使用日期选择器对话框供用户选择日期。 我要做的是在文本视图中显示所选日期。请任何人帮助我获取所选日期,然后相应地显示它。 提前谢谢 以下是我用来实现DatePicker对话框的代码:Android-日期选择器-Obatin值,android,datepicker,Android,Datepicker,我在活动中使用日期选择器对话框供用户选择日期。 我要做的是在文本视图中显示所选日期。请任何人帮助我获取所选日期,然后相应地显示它。 提前谢谢 以下是我用来实现DatePicker对话框的代码: public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialo
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 yyyy, int mm, int dd) {
// Do something with the date chosen by the user
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
在onDateSet方法中
String cuurent_date = String.valueOf(c.get(Calendar.DAY_OF_MONTH))
+ "/" + String.valueOf(c.get(Calendar.MONTH) + 1) + "/"
+ String.valueOf(c.get(Calendar.YEAR));
在onDateSet方法中
String cuurent_date = String.valueOf(c.get(Calendar.DAY_OF_MONTH))
+ "/" + String.valueOf(c.get(Calendar.MONTH) + 1) + "/"
+ String.valueOf(c.get(Calendar.YEAR));
在活动中实现侦听器接口并将其传递给DatePickerFragment。然后通过调用侦听器的方法触发侦听器。此处描述:在活动中实现侦听器接口,并将其传递给DatePickerFragment。然后通过调用侦听器的方法触发侦听器。这里描述了它:将Textview声明为public&static&通过className.textviewName.setText()在日期集方法上调用它;执行此操作时,会出现一个错误-“无法从类型活动中静态引用非静态方法findViewById(int)”,这就是我声明文本视图的方式:public static TextView showSelDate=(TextView)findViewById(R.id.textView3)@RaghavKumar您是否将其声明为全局,表示在onCreate方法之前。如果不是,则在onCreate方法上方声明它,公共静态文本视图showSelDate;&在onCreate()中,showSelDate=(TextView)findViewById(R.id.textView3);将Textview声明为public&static&通过className.textviewName.setText()在日期集方法上调用它;执行此操作时,会出现一个错误-“无法从类型活动中静态引用非静态方法findViewById(int)”,这就是我声明文本视图的方式:public static TextView showSelDate=(TextView)findViewById(R.id.textView3)@RaghavKumar您是否将其声明为全局,表示在onCreate方法之前。如果不是,则在onCreate方法上方声明它,公共静态文本视图showSelDate;&在onCreate()中,showSelDate=(TextView)findViewById(R.id.textView3);