カレンダー画面 … 作成編3

アプリ ソースコード家計簿アプリ

画面遷移

 状態:学習中  閲覧数:2,330  投稿日:2013-07-28  更新日:2013-07-29
一番真上にある「日付/7月22日」クリックすると「カレンダー画面」へ遷移

・インテントのインスタンス生成
・「元画面アクティビティのインスタンス」と「次画面アクティビティのクラス」を明示的に指定
▼/src/android/style/householdaccount/MonthCalendar.java
       // 画面遷移ボタン(日付/7月22日)を押下したとき
      dateButton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                      Intent intent = new Intent(MainActivity.this, MonthCalendar.class);//インテントのインスタンス生成。 「元画面アクティビティのインスタンス」と「次画面アクティビティのクラス」を明示的に指定
                      startActivity(intent);
              }
      });


・Androidマニフェストに、インテント先のクラスを書く
▼/AndroidManifest.xml
<activity android:name="MonthCalendar"></activity>


カレンダー機能

 閲覧数:449 投稿日:2013-07-28 更新日:2013-07-30

考え方


テーブル作成
・横 … 7列。日~土
・縦 … 7行
※先頭行は見出しに付き固定表示。実際に動的生成する個数は、7×6=42
- 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 - - -
- - - - - - -


1.変数設定


▼/src/android/style/householdaccount/MonthCalendar.java
public class MonthCalendar extends Activity implements OnClickListener{

private TextView text1;//2013年7月
private Button button1,button2,button3;//not used
private Button[] button=new Button[10];//button[1]月別,button[2]←,button[3]→
private Button[] button_table=new Button[50];//カレンダー内日付ボタン



2.コンポーネント生成


・onCreateメソッド内で、コンポーネントを42個作成
※コンポーネント=部品。ここではボタンのこと
▼/src/android/style/householdaccount/MonthCalendar.java
        //コンポーネント
       text1=(TextView)this.findViewById(R.id.monthcalendar_text1);//2013年7月
       button[1] = (Button)this.findViewById(R.id.monthcalendar_button1);//月別
       button[2] = (Button)this.findViewById(R.id.monthcalendar_button2);//←
       button[3] = (Button)this.findViewById(R.id.monthcalendar_button3);//→
       button_table[1] = (Button)this.findViewById(R.id.monthcalendar_button1_1);
       button_table[2] = (Button)this.findViewById(R.id.monthcalendar_button1_2);
       button_table[3] = (Button)this.findViewById(R.id.monthcalendar_button1_3);
       button_table[4] = (Button)this.findViewById(R.id.monthcalendar_button1_4);
       button_table[5] = (Button)this.findViewById(R.id.monthcalendar_button1_5);
       button_table[6] = (Button)this.findViewById(R.id.monthcalendar_button1_6);
       button_table[7] = (Button)this.findViewById(R.id.monthcalendar_button1_7);
       button_table[8] = (Button)this.findViewById(R.id.monthcalendar_button2_1);
       button_table[9] = (Button)this.findViewById(R.id.monthcalendar_button2_2);
       button_table[10] = (Button)this.findViewById(R.id.monthcalendar_button2_3);
       button_table[11] = (Button)this.findViewById(R.id.monthcalendar_button2_4);
       button_table[12] = (Button)this.findViewById(R.id.monthcalendar_button2_5);
       button_table[13] = (Button)this.findViewById(R.id.monthcalendar_button2_6);
       button_table[14] = (Button)this.findViewById(R.id.monthcalendar_button2_7);
       button_table[15] = (Button)this.findViewById(R.id.monthcalendar_button3_1);
       button_table[16] = (Button)this.findViewById(R.id.monthcalendar_button3_2);
       button_table[17] = (Button)this.findViewById(R.id.monthcalendar_button3_3);
       button_table[18] = (Button)this.findViewById(R.id.monthcalendar_button3_4);
       button_table[19] = (Button)this.findViewById(R.id.monthcalendar_button3_5);
       button_table[20] = (Button)this.findViewById(R.id.monthcalendar_button3_6);
       button_table[21] = (Button)this.findViewById(R.id.monthcalendar_button3_7);
       button_table[22] = (Button)this.findViewById(R.id.monthcalendar_button4_1);
       button_table[23] = (Button)this.findViewById(R.id.monthcalendar_button4_2);
       button_table[24] = (Button)this.findViewById(R.id.monthcalendar_button4_3);
       button_table[25] = (Button)this.findViewById(R.id.monthcalendar_button4_4);
       button_table[26] = (Button)this.findViewById(R.id.monthcalendar_button4_5);
       button_table[27] = (Button)this.findViewById(R.id.monthcalendar_button4_6);
       button_table[28] = (Button)this.findViewById(R.id.monthcalendar_button4_7);
       button_table[29] = (Button)this.findViewById(R.id.monthcalendar_button5_1);
       button_table[30] = (Button)this.findViewById(R.id.monthcalendar_button5_2);
       button_table[31] = (Button)this.findViewById(R.id.monthcalendar_button5_3);
       button_table[32] = (Button)this.findViewById(R.id.monthcalendar_button5_4);
       button_table[33] = (Button)this.findViewById(R.id.monthcalendar_button5_5);
       button_table[34] = (Button)this.findViewById(R.id.monthcalendar_button5_6);
       button_table[35] = (Button)this.findViewById(R.id.monthcalendar_button5_7);
       button_table[36] = (Button)this.findViewById(R.id.monthcalendar_button6_1);
       button_table[37] = (Button)this.findViewById(R.id.monthcalendar_button6_2);
       button_table[38] = (Button)this.findViewById(R.id.monthcalendar_button6_3);
       button_table[39] = (Button)this.findViewById(R.id.monthcalendar_button6_4);
       button_table[40] = (Button)this.findViewById(R.id.monthcalendar_button6_5);
       button_table[41] = (Button)this.findViewById(R.id.monthcalendar_button6_6);
       button_table[42] = (Button)this.findViewById(R.id.monthcalendar_button6_7);



3.クリック処理


・onCreateメソッド内で、「カレンダー内日付ボタン」に「クリックリスナー」を結びつける
▼/src/android/style/householdaccount/MonthCalendar.java
        for(int i=1;i<=42;i++){
        button_table[i].setOnClickListener(this);
        }



4.レイアウト


レイアウト用XMLファイル生成
▼/res/layout/monthcalendar.xml
<LinearLayout><!--大枠-->
  <LinearLayout><!--一番上ナビ-->
    <Button>月別
    <Button>←
    <TextView>2013年7月
    <Button>→
  </LinearLayout><!--一番上ナビEnd-->




  <LinearLayout> <!--上の直線-->
  </LinearLayout><!--上の直線End-->




  <LinearLayout><!--曜日。見出行-->
  <TextView/>
  <TextView/>
  <TextView/>
  <TextView/>
  <TextView/>
  <TextView/>
  <TextView/>
  </LinearLayout><!--曜日。見出行End-->




  <LinearLayout><!--1行目-->
  <Button/>
  <Button/>
  <Button/>
  <Button/>
  <Button/>
  <Button/>
  <Button/>
  </LinearLayout><!--1行目End-->




  <LinearLayout><!--2行目-->
  <Button/>
  <Button/>
  <Button/>
  <Button/>
  <Button/>
  <Button/>
  <Button/>
  </LinearLayout><!--2行目End-->




  <!--以下略-->




</LinearLayout><!--大枠End-->



5.ボタンに文字(日/1~31)を振る


「Activityクラスを拡張したMonthCalendarクラス」メンバ変数に、現在の日付を取得
※dayOfWeek … 曜日。日曜1、月曜2、火曜3 … 土曜7
▼/src/android/style/householdaccount/MonthCalendar.java
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int dayOfWeek =calendar.get(Calendar.DAY_OF_WEEK);//曜日


        //当月初日(1日)に関する設定
       calendar.set(year,month,1);//日付を、当月初日(1日)に設定
       day=calendar.get(Calendar.DAY_OF_MONTH);//当月初日の日付(1日)取得
       dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);//当月初日(1日)の曜日取得
       
       //月末日付に関する設定
       calendar.add(Calendar.MONTH, 1);//設定した日付のMONTH(月)に1を足す。つまり翌月1日に設定
       calendar.add(Calendar.DATE, -1);//設定した日付(翌月1日)のDATE(日)から1を引く。つまり当月末日に設定
       int lastDate = calendar.get(Calendar.DATE);//この末日をlastDate変数へ格納
       
       //日1,月2,・・・土7。dayOfWeekは当月初日曜日。5なら5番目から始める、という意味。これを当月末日まで繰り返す
for(int i=dayOfWeek;i<=dayOfWeek+lastDate-1;i++){
button_table[i].setText(String.valueOf(day));
button_table[i].setTextSize(15);
day=day+1;
}



6.前月ボタン(←)クリック処理


上にある左矢印←(button[2])を押した時のクリック処理
・前の月に戻る処理
    	else if (v==button[2]){//上の←。前の月に戻る処理
    if(month==0){//もし今が1月なら(monthは+1で表示されるため)、
    year=year-1;//yearを1引く
    month=11;//monthを11(12月)にする
    }
    else{//それ以外の月なら
    month=month-1;//普通にmonthから1を引く
    }


上記で取得した前月日付をタイトルへセット
    		text1.setText(String.valueOf(String.valueOf(year+"年"+(month+1)+"月")));//上記で取得した前月日付をタイトルへセット


前月日付に合わせ、カレンダー内の日付を振りなおす
・「5.ボタンに文字(日/1~31)を振る」処理をもう一度書く
さっきと少し違うのは、初日より前のボタンと、
末日より後のボタンには空白文字を入れる点です。こうしないと文字がだぶってしまいました
・試しに「前後の空白文字入れ処理」をコメントアウトしてみたけど、正常表示される
・エミュレータだから?
・同一処理なら、メソッドとしてまとめた方が良いかもしれない


7.翌月ボタン(→)クリック処理


・同上


カレンダー機能

「カレンダー内日付ボタン」クリック処理

 閲覧数:414 投稿日:2013-07-30 更新日:2013-07-30

概要


・「カレンダー内日付ボタン」クリックすると、メイン画面へ遷移


1.変数設定


▼/src/android/style/householdaccount/MonthCalendar.java
・onClickメソッド内
//      「カレンダー内日付ボタン」クリック処理
       
//当月初日(1日)に関する設定          
       calendar.set(year,month,1);//日付を、当月初日(1日)に設定
       day=calendar.get(Calendar.DAY_OF_MONTH);//当月初日の日付(1日)取得
       dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);//当月初日(1日)の曜日取得
       
       //月末日付に関する設定              
       calendar.add(Calendar.MONTH, 1);//設定した日付のMONTH(月)に1を足す。つまり翌月1日に設定
       calendar.add(Calendar.DATE, -1);//設定した日付(翌月1日)のDATE(日)から1を引く。つまり当月末日に設定
       int lastDate = calendar.get(Calendar.DATE);//この末日をlastDate変数へ格納

       calendar.set(year,month,1);



2.インテント処理


intentを使用し、MainActivityクラス(最初に表示するメインクラス)へ変数を渡す
▼/src/android/style/householdaccount/MonthCalendar.java
        //日1,月2,・・・土7。dayOfWeekは当月初日曜日。5なら5番目から始める、という意味。つまり初日から月末までをfor文で繰り返し処理
       for(int i=dayOfWeek;i<=dayOfWeek+lastDate-1;i++){
        if(v==button_table[i]){//そのボタンをクリックしたとき
        Intent intent=new Intent();
        intent.setClass(MonthCalendar.this,MainActivity.class);//intentでMainActivityクラス(最初に表示するメインクラス)へ持っていく
        intent.putExtra("year",year);//年を"year"へ格納
        intent.putExtra("month",month);//月を"month"へ格納
        intent.putExtra("day", day);//日を"day"へ格納
        startActivity(intent);
        }
    day=day+1;
       }



3.メイン画面に反映


▼/src/android/style/householdaccount/MainActivity.java
onCreateメソッド内
・getExtraメソッドで先ほど格納した"year"、"month"、"day"を取出
       Intent intent1 = getIntent();
      year = intent1.getIntExtra("year",year);
      month = intent1.getIntExtra("month",month);
      day = intent1.getIntExtra("day",day);


・なお、リンク先ではここで、それぞれプリファレンスとテーブルネームに設定しているが、実際のコード上では「intentFileName」メソッドにまとめ、複数から呼び出される実装になってている
	public String[] intentFileName(){
int[] calendar=intentCalendar();
       //第一引数はキー。第二引数はキーが存在しなかった場合の戻り値。
       String prefName="pref";
       String tableName=String.valueOf(calendar[0])+"-"+String.valueOf(calendar[1]+1)+"-"+String.valueOf(calendar[2]);
       String tableMonthName=String.valueOf(calendar[0])+"-"+String.valueOf(calendar[1]+1);
       String[] fileName={prefName,tableName,tableMonthName};
       return fileName;
}

・メソッドに切り出すのは分かるが、どうして複数個所から呼ばれているかは不明
・インテント内容をgetExtraメソッドで取得した直後に設定すれば、後はもう設定しなくても良いとは思うけど、そうではないのかしら?


項目入力画面への遷移 … 作成編2

インテント … 項目画面からのデータをメイン画面に反映(DB未利用) … 作成編4



週間人気ページランキング / 5-12 → 5-18
順位 ページタイトル抜粋 アクセス数
アクセスが、ありませんでした! 0
2024/5/19 1:01 更新