完全な例が役立つかもしれません:
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MonthActivity extends Activity {
private Calendar calendar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
calendar = Calendar.getInstance();
calendar.setTime(new Date());
final TextView textView = (TextView) findViewById(R.id.dateTextView);
updateTextView(textView);
Button prev = (Button) findViewById(R.id.prevCmd);
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.MONTH, -1);
updateTextView(textView);
}
});
Button next = (Button) findViewById(R.id.nextCmd);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.MONTH, 1);
updateTextView(textView);
}
});
}
private void updateTextView(TextView textView) {
textView.setText(""
+ calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT,
Locale.getDefault()));
}
}
およびレイアウトXMLファイル
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout12"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/prevCmd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text=" < " />
<TextView
android:id="@+id/dateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/nextCmd"
android:layout_toRightOf="@+id/prevCmd"
android:gravity="center_vertical|center_horizontal"
android:paddingTop="8dp"
android:text="Jul"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="@+id/nextCmd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text=" > " />
</RelativeLayout>