如何在android中获得月份和年份? (当我们点击左箭头和右箭头时,按升序和降序排列)。
我附上截图供参考
https://s28.postimg.org/gyuzzumkd/time_date.jpg
我使用了他的0x5f3759df答案中的一些代码:
我的代码:
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Fragment_A extends Fragment implements OnClickListener {
private ListView list;
ImageButton next,previous;
ImageView newmemory;
TextView tv;
public Fragment_A() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_a, container, false);
// call the views with this layout
list = (ListView) rootview.findViewById(R.id.listView1);
next = (ImageButton) rootview.findViewById(R.id.ibLeftarrow);
previous = (ImageButton) rootview.findViewById(R.id.ibRightarrow);
newmemory = (ImageView)rootview.findViewById(R.id.ivNewMemory);
tv = (TextView) rootview.findViewById(R.id.tvMonth);
next.setOnClickListener(this);
previous.setOnClickListener(this);
newmemory.setOnClickListener(this);
CustomAdapterA adapter = new CustomAdapterA(getActivity());
list.setAdapter(adapter);
return rootview;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.ibLeftarrow)
{
createDialogWithoutDateField().show();
}
else if (v.getId()==R.id.ibRightarrow)
{
createDialogWithoutDateField().show();
}
else if (v.getId()==R.id.ivNewMemory)
{
Intent i = new Intent(getActivity(), NewMemory.class);
startActivity(i);
}
}
private DatePickerDialog createDialogWithoutDateField(){
DatePickerDialog dpd = new DatePickerDialog(getActivity(), datePickerListener ,2014,1, 24);
try{
java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();
for (java.lang.reflect.Field datePickerField : datePickerFields) {
Log.i("test", datePickerField.getName());
if ("mDaySpinner".equals(datePickerField.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
}catch(Exception ex){
}
return dpd;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
int year = selectedYear;
int month = selectedMonth;
tv.setText(new StringBuilder().append(month+1)
.append(" ").append(year)
.append(" "));
}
};
}
答案 0 :(得分:1)
按下上一步按钮
if (mCalendar.get(Calendar.MONTH) == mCalendar
.getActualMinimum(Calendar.MONTH)) {
mCalendar.set((mCalendar.get(Calendar.YEAR) - 1),
mCalendar.getActualMaximum(Calendar.MONTH), 1);
} else {
mCalendar.set(Calendar.MONTH,
mCalendar.get(Calendar.MONTH) - 1);
}
按下“下一步”按钮
if (mCalendar.get(Calendar.MONTH) == mCalendar
.getActualMaximum(Calendar.MONTH)) {
mCalendar.set((mCalendar.get(Calendar.YEAR) + 1),
mCalendar.getActualMinimum(Calendar.MONTH), 1);
} else {
mCalendar.set(Calendar.MONTH,
mCalendar.get(Calendar.MONTH) + 1);
}
在onCreate()
Calendar mCalendar = Calendar.getInstance();
获取月份和年份
String str = android.text.format.DateFormat.format("MMMM yyyy", mCalendar).toString();
答案 1 :(得分:0)
您可以使用getInstance()函数获取日历日期,然后根据按钮事件侦听器(左右箭头)将年份和月份解析为String数组。下面是getinstance()函数的代码:
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
答案 2 :(得分:0)
dateFormat = new SimpleDateFormat("dd-MMMM-yyyy", locale);
c = Calendar.getInstance();
String date = dateFormat.format(c.getTime());
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplayLeaveDate();
}
private void updateDisplayLeaveDate() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, mYear);
cal.set(Calendar.MONTH, mMonth);
cal.set(Calendar.DAY_OF_MONTH, mDay);
dateMorning = cal.getTime();
String date = dateFormat.format(dateMorning);
tvLeaveDateDisplay.setText(date);
leaveDate = Util.getDateFormat(mDay, mMonth, mYear);
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListenerMorning = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplayLeaveDate();
}
};