问题描述
我正在试图找出如何实现数据和时间选择器和我正在构建的应用程序。我似乎找不到一种内置的方式来允许用户设置他们退出的日期。有没有一个优雅的方式实现一个?为了计算毫秒,我需要能够提取日,月,年,小时和小时的值。任何有关实现此目的的建议?对于PreferenceScreen中的时间/日期选择器,可以使用onPreferenceClickListener偏爱)。在PreferenceActivity中使用DatePickerDialog.OnDateSetListener或TimePickerDialog.OnTimeSetListener来检索选定的日期/时间。
public class SettingsActivity extends PreferenceActivity implements DatePickerDialog。 OnDateSetListener {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
首选项btnDateFilter =(首选项)findPreference(btnDateFilter);
btnDateFilter.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onPreferenceClick(偏好偏好){
showDateDialog();
return false;
}
});
@Override
public void onDateSet(DatePicker datePicker,int i,int i2,int i3){
Log.i(dasd,year+ i +month+ i2 +day+ i3);
}
private void showDateDialog(){
//使用当前日期作为选择器中的默认日期
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);
new DatePickerDialog(this,this,year,month,day).show();
}
}
preference.xml
<首选项
android:key =btnDateFilter
android:summary =使用当前设置重新安排
android:title =重新安排/>
I'm trying to figure out how to implement a Data and Time picker for and app I'm building. I can't seem to find a built in way to allow the user to set the date they quit. Is there an elegant way to implement one? I'll need to be able to pull the day, month, year, hour, and minute values in order to calculate milliseconds. Any advice on what's available to achieve this?
For time/date picker within PreferenceScreen, you can invoke the dialog using onPreferenceClickListener (Preference). Use DatePickerDialog.OnDateSetListener or TimePickerDialog.OnTimeSetListener within PreferenceActivity to retrieve the selected date/time.
public class SettingsActivity extends PreferenceActivity implements DatePickerDialog.OnDateSetListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Preference btnDateFilter = (Preference) findPreference("btnDateFilter");
btnDateFilter.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onPreferenceClick(Preference preference) {
showDateDialog();
return false;
}
});
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i2, int i3) {
Log.i("dasd","year "+i+" month "+i2+" day "+i3);
}
private void showDateDialog(){
// 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);
new DatePickerDialog(this,this, year, month, day).show();
}
}
preference.xml
<Preference
android:key="btnDateFilter"
android:summary="Reschedule using current setting"
android:title="Reschedule" />
这篇关于在Android的首选项屏幕中使用TimePicker或DatePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!