在我的应用程序中,我有一个文本视图来设置当月和下个月和上个月的两个按钮。当点击下一个按钮时,我需要将下个月设置为文本视图而不打开月份选择器,也类似于上一个按钮。请任何人帮助我。
答案 0 :(得分:2)
Calendar c= Calendar.getInstance();
int next_month = c.get(Calendar.MONTH) + 1;
int prev_month = c.get(Calendar.MONTH) - 1;
答案 1 :(得分:0)
试着这个来获取下个月的名字:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
String name = cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
答案 2 :(得分:0)
你也可以使用像getMonth()这样的方法 - 这个方法获得选定的月份。这将有助于找到当前月份,然后通过添加一些功能按照上一个或下一个按钮更改月份。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button previous,nextmonth;
TextView calenderview;
private int year, month, day;
private Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
previous = (Button) findViewById(R.id.previous_button);
nextmonth = (Button) findViewById(R.id.next_button);
calenderview = (TextView) findViewById(R.id.calender);
calenderview.setText(getCurrentDate());
previous.setOnClickListener(this);
nextmonth.setOnClickListener(this);
}
public String getCurrentDate(){
StringBuilder builder=new StringBuilder();
builder.append("Current Month: ");
builder.append(Calendar.MONTH);
return builder.toString();
}
private String getnextmonth() {
StringBuilder builder=new StringBuilder();
builder.append("Previous Month: ");
builder.append(Calendar.MONTH+1);
return builder.toString();
}
private String getpreviousmonth() {
StringBuilder builder=new StringBuilder();
builder.append("Next Month : ");
builder.append(Calendar.MONTH-1);
return builder.toString();
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.previous_button:
calenderview.setText(getpreviousmonth());
break;
case R.id.next_button:
calenderview.setText(getnextmonth());
break;
}
}
}