我正在寻找复制 android 开发者应用教程中显示的功能。
我已经实现了正确的布局,但是任何人都可以帮助我让微调器能够选择日期和时间。除非它是硬编码的,但我假设 android 内置了一些日期/时间设施。
有任何想法吗?
我正在寻找复制 android 开发者应用教程中显示的功能。
我已经实现了正确的布局,但是任何人都可以帮助我让微调器能够选择日期和时间。除非它是硬编码的,但我假设 android 内置了一些日期/时间设施。
有任何想法吗?
谁能帮我让微调器能够选择日期和时间。
使用Pickers
android api 中提供的这些将为您提供帮助用户选择日期和时间所需的小部件。
我用过 Joda 日期时间。另外,我在微调器中创建了自己的项目视图 -R.layout.spinner_item
public class YourFragment extends Fragment {
private DateSpinnerAdapter daysAdapt;
private DateSpinnerAdapter monthsAdapt;
private DateSpinnerAdapter yearsAdapt;
private static final int MIN_YEAR = 1935;
private static final int YEARS_RANGE = 61;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initSpinners();
}
private void initSpinners() {
continueButton.setOnClickListener(this);
monthsAdapt = new DateSpinnerAdapter(getActivity(), R.layout.spinner_item);
monthsAdapt.setData(getActivity().getResources().getStringArray(R.array.monthsArray));
monthSpinner.setAdapter(monthsAdapt);
setMonthsListener();
yearsAdapt = new DateSpinnerAdapter(getActivity(), R.layout.spinner_item);
yearsAdapt.setData(getYears());
yearsSpinner.setAdapter(yearsAdapt);
setYearsListener();
daysAdapt = new DateSpinnerAdapter(getActivity(), R.layout.spinner_item);
setDaysSpinnerValues(monthSpinner.getSelectedItemPosition(), yearsSpinner.getSelectedItemPosition());
daysSpinner.setAdapter(daysAdapt);
}
private void setMonthsListener() {
monthSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int number, long arg3) {
setDaysSpinnerValues(number, yearsSpinner.getSelectedItemPosition() + MIN_YEAR);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void setYearsListener() {
yearsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int number, long arg3) {
setDaysSpinnerValues(monthSpinner.getSelectedItemPosition(), number + MIN_YEAR);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private int getMonthOfBirth() {
return monthSpinner.getSelectedItemPosition() + 1;
}
private int getYearOfBirth() {
return yearsSpinner.getSelectedItemPosition() + MIN_YEAR;
}
private int getDayOfBirth() {
return daysSpinner.getSelectedItemPosition() + 1;
}
private DateTime getBirthday() {
return DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(getStringDate());
}
private String getStringDate() {
return getYearOfBirth() + "-" + getMonthOfBirth() + "-" + getDayOfBirth();
}
private String[] getYears() {
String[] years = new String[YEARS_RANGE];
for(int i = 0; i < YEARS_RANGE; i++) {
years[i] = "" + (i + MIN_YEAR);
}
return years;
}
private String[] getDays(int month, int year) {
int maxDays = getDaysInMonth(month, year);
String[] days = new String[maxDays];
for(int i = 0; i < maxDays; i++) {
days[i] = "" + (i + 1);
}
return days;
}
public int getDaysInMonth(int month, int year) {
Calendar cal = new GregorianCalendar();
cal.set(year, month, 1); //note that the month in Calendar goes from 0-11
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
public void setDaysSpinnerValues(int month, int year) {
daysAdapt.clear();
daysAdapt.setData(getDays(month, year));
daysAdapt.notifyDataSetChanged();
}
}
和适配器:
public class DateSpinnerAdapter extends ArrayAdapter<String>{
protected FragmentActivity context;
public DateSpinnerAdapter(FragmentActivity context, int resource) {
super(context, resource);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View element = super.getView(position, convertView, parent);
((TextView) element).setTextColor(context.getResources().getColor(R.color.white));
LinearLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
element.setLayoutParams(layoutParams);
return element;
}
@SuppressLint("ResourceAsColor")
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View element =super.getDropDownView(position, convertView, parent);
element.setPadding(15, 30, 0, 30);
return element;
}
@TargetApi(11)
public void setData(String[] data) {
clear();
if (data != null) {
//If the platform supports it, use addAll, otherwise add in loop
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
addAll(data);
}else{
for(String item: data){
add(item);
}
}
}
}
}