-1

使用此代码,我在微调器中查看当前年份,现在我还想查看当前月份。如何?谢谢

    if (years.size() == 0){
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
        years.add(currentYear+"/"+currentMonth);
    }

    SpinnerAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, years);

    yearSelector.setAdapter(adapter);
    yearSelector.setSelection(0);
    yearSelector.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view,
                int position, long id) {
            String anno = years.get(position);

            showDetails(anno);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            return;
        }
4

1 回答 1

2

您可以通过

int currentYear = Calendar.getInstance().get(Calendar.YEAR);
int currentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
years.add(currentYear+"/"+currentMonth);

由于基于 android 的月份为零,因此您必须加 1 才能获得当前月份

于 2013-09-28T20:20:50.127 回答