素人プログラマ奮闘記

javaの初心者がAndroidのことを独学で勉強しつつ、メモを残していきます。

CalenderMemoソフト作成(1)

今回は画面にカレンダーを表示して、選択中の日付に対して、メモをして保存する機能があるソフトを作成してみたいと思います。 もちろんすでに世の中には素晴らしいカレンダー表示ソフトはたくさんあるのは知っていますが、どうしても自作でつくりたかったので(笑)

画面にカレンダーを表示する方法として
android.widget.CalendarViewを使うか、自分で作るかで大きく違ってきます。

今回は勉強ということと自作した方が後で色々細かいところを調整したい時に融通がきくので
1から自分で作ってみようと思います。

textviewに枠線を表示する方法

カレンダーデザインを考えてる時に、表みたいなものを作ろうと思いTextViewを並べてみたのですが
枠線を表示しないと表っぽくなりません。

TextViewのプロパティには、枠線表示らしき項目がありませんでした。
ググってみたところmain.xml(画面レイアウトファイル)とは別に枠線を表示するためのxmlファイルを作成してそのファイルパスを
android:backgroundプロパティにセットすることにより枠線が表示されるようです。

枠線を表示するためのxmlファイルを作成方法ですが
パッケージエクスプローラ上でマウスの右クリック>新規>Android XML ファイル>を選択します。

次に、リソースタイプにDrawableを選択、プロジェクト名は現在作成中のプロジェクトを選択
ファイル名はtext_day_line.xmlを設定して、完了ボタンを押します。

すろとファイルが /res/drawable/text_day_lien.xmlに作成されているはずです。

※公式からの引用
Drawable リソースは画面上に描画でき、
getDrawable(int) のような API で取得でき、
android:drawable や android:icon のような属性を使用して他の XML リソースに適用できるグラフィックの一般的な概念です。
text_day_lien.xmlを下記の通りに変更してください。


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#FFFFFF" />
    <stroke android:width="1px" android:color="#CCCCCC" />
    <padding android:left="2px" android:top="2px"
    android:right="2px" android:bottom="2px" />
    <corners android:radius="1px" />
</shape>	
■各属性の意味
●shape
  shapeは「形状」という意味です。
●solid 
  android:colorで枠線の内側の塗りつぶし色を設定
●stroke
  android:widthで枠線の幅を設定
  android:colorで線の色を設定
   ●padding
  内側と実際の要素の余白を定義できます。
●corners android:radius
  枠線の角の設定を行います。

各テキストごとにより塗りつぶし色設定を変えたかったので、3種類作りました。
text_day_line.xml //日表示テキスト
text_month_line.xml //曜日表示テキスト
text_week_line.xml //月表示テキスト

あとはmain.xmlで定義するTextViewのBackGroundResourceプロパティに定義する。

<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
:background="@drawable/text_day_lien.xml"
 android:text="Test"
/>
上記を踏まえたうえでmain.xmlを作成します。
年、月、曜日、日表示は単純にテキストボックスをずらっと並べて作成します。
次の月、前の月へ移動できるようにボタンを設けます。

main.xmlの内容ですが、ソースをすべて載せるととんでもない行数になるため
一部抜粋で載せます。


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
	<LinearLayout 
		android:layout_width="match_parent"
		android:layout_height="0dp"            
		android:layout_weight="2"
		android:background="@drawable/text_month_line"
        android:orientation="horizontal" >        
		<Button 
	   	    android:id="@+id/previous_month_button"
			android:layout_width="0dp"
            android:layout_height="fill_parent"            
			android:layout_weight="1"
			android:gravity="fill"
        	android:text="前の月"
        	/>	    	    
        	<TextView
			android:id="@+id/header_month_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"            
			android:layout_weight="2"
			android:gravity="fill"
			android:textSize="25sp"
            android:text="2013年10月" 
            />
		<Button 
	   	    android:id="@+id/next_month_id"
			android:layout_width="0dp"
            android:layout_height="fill_parent"            
			android:gravity="fill"
			android:layout_weight="1"
        	android:text="次の月"
        	/>	    	    	
    </LinearLayout>
	<LinearLayout 
		android:layout_width="match_parent"
		android:layout_height="0dp"            
		android:layout_weight="1"
        android:orientation="horizontal" >        
	    <TextView
			android:id="@+id/header_su_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"            
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_week_line"
            android:text="日" />
		<TextView
			android:id="@+id/header_mo_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_week_line"
            android:text="月" />
	    <TextView
			android:id="@+id/header_tu_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_week_line"
            android:text="火" />
		<TextView
			android:id="@+id/header_we_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_week_line"
            android:text="水" />
	    <TextView
			android:id="@+id/header_th_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_week_line"
            android:text="木" />
	    <TextView
			android:id="@+id/header_fr_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_week_line"
            android:text="金" />
	    <TextView
			android:id="@+id/header_sa_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_week_line"
            android:text="土" />
	</LinearLayout>
	<LinearLayout 
		android:layout_width="match_parent"
		android:layout_height="0dp"            
		android:layout_weight="3"
        android:orientation="horizontal" >        
	    <TextView
			android:id="@+id/one_su_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"            
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="1" />
		<TextView
			android:id="@+id/one_mo_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="2" />
	    <TextView
			android:id="@+id/one_tu_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="3" />
		<TextView
			android:id="@+id/one_we_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="4" />
	    <TextView
			android:id="@+id/one_th_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="5" />
	    <TextView
			android:id="@+id/one_fr_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="6" />
	    <TextView
			android:id="@+id/one_sa_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="7" />
	</LinearLayout>
	<LinearLayout 
		android:layout_width="match_parent"
		android:layout_height="0dp"            
		android:layout_weight="3"
        android:orientation="horizontal" >        
	    <TextView
			android:id="@+id/tow_su_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"            
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="1" />
		<TextView
			android:id="@+id/tow_mo_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="2" />
	    <TextView
			android:id="@+id/tow_tu_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="3" />
		<TextView
			android:id="@+id/tow_we_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="4" />
	    <TextView
			android:id="@+id/tow_th_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="5" />
	    <TextView
			android:id="@+id/tow_fr_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="6" />
	    <TextView
			android:id="@+id/tow_sa_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="7" />
	</LinearLayout>
	<LinearLayout 
		android:layout_width="match_parent"
		android:layout_height="0dp"            
		android:layout_weight="3"
        android:orientation="horizontal" >        
	    <TextView
			android:id="@+id/three_su_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"            
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="1" />
		<TextView
			android:id="@+id/three_mo_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="2" />
	    <TextView
			android:id="@+id/three_tu_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="3" />
		<TextView
			android:id="@+id/three_we_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="4" />
	    <TextView
			android:id="@+id/three_th_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="5" />
	    <TextView
			android:id="@+id/three_fr_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="6" />
	    <TextView
			android:id="@+id/three_sa_text"
			android:layout_width="0dp"
            android:layout_height="fill_parent"
			android:layout_weight="1"
			android:gravity="center"
			android:background="@drawable/text_day_line"
            android:text="7" />
	</LinearLayout>	

画面イメージはこんな感じになります。


今回はここまで

カテゴリーへ


CalenderMemoソフト作成(2)

今回はカレンダーの情報を二次元配列にしてくれる機能を持った、クラスを作成していきます。

javaには大変便利な日付計算用のjava.util.Calendarというクラスが提供されているため
月末や曜日を求めるといったことは このクラスを応用すれば比較的簡単に実行できます。

年と月を渡すと、5週間の5列と日曜~土曜の7列の二次元配列を作成して、そこに日付を入れていくようにします。
月の始まりの曜日が水曜からだった場合、一週目は日,月,火は日付がはいらないためその場所には0を設定します。

2013年10月の場合の2次元配列データイメージはこんな感じです
|00|00|01|02|03|04|05|
|06|07|08|09|10|11|12|
|13|14|15|16|17|18|19|
|20|21|22|23|24|25|26|
|27|28|29|30|31|00|00|

ソースの中身は下記の通り

				
package com.example.calendartest;

import java.util.Calendar;

/**
 * カレンダーフィールド作成クラス
 */
public class CalendarInfo {
	private int year;       //対象の西暦年
	private int month;      //対象の月
	private int startDay;   //先頭曜日
	private int lastDate;   //月末日付
	public int[][] calendarMatrix = new int[5][7];	//カレンダー情報テーブル

	/**
	 * カレンダー表オブジェクトを作成します。
	 * @param year 西暦年(..., 2005, 2006, 2007, ...)
	 * @param month 月(1, 2, 3, ..., 10, 11, 12)
	 */
	public CalendarInfo(int year, int month) {
		this.year = year;
		this.month = month;
		this.createFields();
	}
	/**
	 * カレンダーフィールド作成
	 */
	private void createFields() {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		
		// 月の初めの曜日を求めます。
		calendar.set(year, month - 1, 1); // 引数: 1月: 0, 2月: 1, ...
		this.startDay = calendar.get(Calendar.DAY_OF_WEEK);//曜日を取得
		
		// 月末の日付を求めます。
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DATE, -1);
		this.lastDate = calendar.get(Calendar.DATE);//日を取得
		int dayCount = 1;
		boolean isStart = false;
		boolean isEnd = false;
		
		for(int i = 0; i < 5; i++) {
			for(int j = 0; j < 7 ; j++) {
				//初期値セット
				this.calendarMatrix[i][j] = 0;
				
				//先頭曜日確認
				// startDay: 日曜日 = 1, 月曜日 = 2, ...
				if(isStart == false && (this.startDay -1 ) == i) {
					//日にちセット開始
					isStart = true;
				}
				
				if(isStart) {
					//終了日まで行ったか
					if(!isEnd) {
						this.calendarMatrix[i][j] = dayCount;
					}
					
					//カウント+1
					dayCount++;
					  
					//終了確認
					if(dayCount > this.lastDate) {
						isEnd = true;
					}		    		
				}
			}
		}
	}
}


別にAndroidだからといって、特別難しい処理はしていません(笑)
これを使って、レイアウトに配置したテキストビューの配列に値をほりこんでいこうと思います。
今回はここまで

カテゴリーへ


CalenderMemoソフト作成(3)

今回は画面上に配置したテキストを管理するクラスを作成しました。
MainActivityで各textviewとこのクラスを紐つけて管理していこうと思います。
ソースの中身は下記の通り

				
package com.example.calendartest;

import android.widget.TextView;

/**
 * テキストコントロールクラス
 */
public class DayTextViewInfo {

	private int textViewId = 0;					//テキストビューID
	private TextView textObject = null;			//テキストオブジェクト
	private int dayNum = 0;						//設定日付
	private boolean isNowDay = false;			//当日フラグ
	private boolean isSelected = false;			//選択フラグ
	
	/**
	 * コンストラクタ
	 */
	public DayTextViewInfo(int controlId){
		this.setTextViewId(controlId);
	}
	
	/**
	 * @return textViewId
	 */
	public int getTextViewId() {
		return textViewId;
	}
	/**
	 * @param textViewId セットする textViewId
	 */
	public void setTextViewId(int textViewId) {
		this.textViewId = textViewId;
	}

	/**
	 * textObject 取得
	 * @return textObject
	 */
	public TextView getTextObject() {
		return textObject;
	}
	/**
	 * @param textObject 設定 textObject
	 */
	public void setTextObject(TextView textObject) {
		this.textObject = textObject;
	}

	/**
	 * dayNum 取得
	 * @return dayNum
	 */
	public int getDayNum() {
		return dayNum;
	}
	/**
	 * @param dayNum 設定 dayNum
	 */
	public void setDayNum(int dayNum) {
		this.dayNum = dayNum;
	}

	/**
	 * isNowDay 取得
	 * @return isNowDay
	 */
	public boolean isNowDay() {
		return isNowDay;
	}
	/**
	 * @param isNowDay 設定 isNowDay
	 */
	public void setNowDay(boolean isNowDay) {
		this.isNowDay = isNowDay;
	}
	
	/**
	 * isSelected 取得
	 * @return isSelected
	 */
	public boolean isSelected() {
		return isSelected;
	}
	/**
	 * @param isSelected 設定 isSelected
	 */
	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}
		/**
	 * 表示文字列を返す
	 * @return
	 */
	public String getDispString() {
		
		if(this.dayNum != 0) {
			return String.valueOf(this.dayNum) + "\n";
		}
		else {
			return "";
		
		}
	}
}

各テキストビューに設定するプロパティは、日付、選択フラグ、当日フラグです。

画面をタッチしたときに選択フラグをONにしてほかのテキストビューと見分けがつくようにします。

ちなみに型がbooleanであるプロパティについてはGetterとして"isPropertyName()"というメソッドでも良いことになっているようです。
"is~"と"get~"が両方ある場合は"is~"が優先されることが多いらしいです。
私的にはすべてgettersetterで統一した方が見やすいかともうのですが(;^ω^)
規約に追記しておきます。

今回はここまで

カテゴリーへ


CalenderMemoソフト作成(4)

今回はMainActivityの実装をしていきます。

まずは、各画面のコントロール操作のオブジェクト格納用などの変数名を決めます
ネーミングセンスがないのは許してください(ノД`)

				
	private Button nextMonthButton = null;				//次の月へボタン
	private Button previousMonthButton = null;			//前の月へボタン
	private TextView headerMonthText = null;			//年月表示テキストビュー
			
	private int currentYear = 0;						//現在表示中の年
	private int currentMonth = 0;						//現在表示中の月
	
	private int nowYear = 0;							//現在の年
	private int nowMonth = 0;							//現在の月
	private int nowDay = 0;								//現在の日
	
	//日表示テキスト情報リスト
	private ArrayList<DayTextViewInfo> dayTextList = new ArrayList<DayTextViewInfo>();


currentYear,currentMonthは現在表示中の年月を
nowYear,nowMonth,nowDayは現在日付を持ちます。

dayTextListは画面上に配置した日付表示用テキストの配列になります。

次に、各コントロールの初期設定をする関数をつくります。
このメソッドをonCreate内で呼びます。
		
	/**
	 * 各コントロール初期化
	 */
	private void initializeControl() {
				
		this.nextMonthButton = (Button)findViewById(R.id.next_month_id);
		this.nextMonthButton.setOnClickListener(this);
		this.previousMonthButton = (Button)findViewById(R.id.previous_month_button);
		this.previousMonthButton.setOnClickListener(this);
		
		this.headerMonthText = (TextView)findViewById(R.id.header_month_text);
		
		DayTextViewInfo info = null;
		
		info = new DayTextViewInfo(R.id.one_su_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.one_mo_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.one_tu_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.one_we_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.one_th_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.one_fr_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.one_sa_text);
		this.dayTextList.add(info);
		
		info = new DayTextViewInfo(R.id.tow_su_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.tow_mo_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.tow_tu_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.tow_we_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.tow_th_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.tow_fr_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.tow_sa_text);
		this.dayTextList.add(info);

		info = new DayTextViewInfo(R.id.three_su_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.three_mo_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.three_tu_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.three_we_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.three_th_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.three_fr_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.three_sa_text);
		this.dayTextList.add(info);

		info = new DayTextViewInfo(R.id.four_su_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.four_mo_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.four_tu_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.four_we_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.four_th_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.four_fr_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.four_sa_text);
		this.dayTextList.add(info);

		info = new DayTextViewInfo(R.id.five_su_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.five_mo_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.five_tu_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.five_we_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.five_th_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.five_fr_text);
		this.dayTextList.add(info);
		info = new DayTextViewInfo(R.id.five_sa_text);
		this.dayTextList.add(info);
						
	    Calendar cal1 = Calendar.getInstance();  			//(1)オブジェクトの生成

	    this.currentYear = cal1.get(Calendar.YEAR);        //(2)現在の年を取得
	    this.currentMonth = cal1.get(Calendar.MONTH) + 1;  //(3)現在の月を取得
	    
	    this.nowYear = this.currentYear;
	    this.nowMonth = this.currentMonth;
	    this.nowDay = cal1.get(Calendar.DATE);         		//(4)現在の日を取得
				
		int id = 0;
		for(int i = 0; i < 5; i++) {
			for(int j = 0; j < 7 ; j++){
				TextView tv = (TextView)findViewById(this.dayTextList.get(id).getTextViewId());
				tv.setOnClickListener(this);
				tv.setBackgroundResource(R.drawable.text_day_line);
				if(j == 0) {
					//日曜日
					tv.setTextColor(Color.RED);
				}				
				if(j == 6) {
					//土曜日
					tv.setTextColor(Color.BLUE);					
				}
				this.dayTextList.get(id).setTextObject(tv);
				id++;
			}
		}
		
		this.SetCalendar(0);
	}				
各コントロールの紐つけをした後で、
setBackgroundResourceを使ってテキストのレイアウトと、クリックイベント登録を設定した後に、
土曜日と日曜日のフォント色をsetTextColorで変更しています。
最後にSetCalendar(0)を呼び出しています。これは日付テキスト表示用の関数です。
	
	/**
	 * カレンダー再描画
	 */
	private void SetCalendar(int offset) {
		this.currentMonth += offset;
		
		if(currentMonth > 12){
			this.currentYear += 1;
			this.currentMonth = 1;
		}
		else if(currentMonth == 0){
			this.currentMonth = 12;
			this.currentYear -= 1;
		}
	    		
		//テキスト表示情報初期化
		for(int i = 0 ; i < this.dayTextList.size(); i++) {
			DayTextViewInfo tg = this.dayTextList.get(i);
			if(tg.isNowDay() || tg.isSelected() ) {
				tg.getTextObject().setBackgroundResource(R.drawable.text_day_line);
			}				

			tg.setNowDay(false);
			tg.setDayNum(0);
			tg.setSelected(false);
			tg.getTextObject().setText(tg.getDispString());
		}

		//カレンダーテーブル作成
		CalendarInfo cl = new CalendarInfo(currentYear, currentMonth);
		
		int row = 0;
		int col = 0;
		for(int i = 0 ; i < this.dayTextList.size(); i++) {
			DayTextViewInfo tg = this.dayTextList.get(i);
			
			if(cl.calendarMatrix[row][col] != 0) {			
				// 日付表示
				tg.setDayNum(cl.calendarMatrix[row][col]);
				tg.getTextObject().setText(tg.getDispString());
				if(this.nowYear == this.currentYear 
					&& this.nowMonth == this.currentMonth 
					&& cl.calendarMatrix[row][col] == nowDay) {
					
					// 当日日付表示
					this.dayTextList.get(i).setNowDay(true);
					tg.getTextObject().setBackgroundResource(R.drawable.text_now_line);
				}
			}
			
			col += 1;
			if(col == 7){
				row += 1;
				col = 0;
			}			
		}
		
		//年月表示
		this.headerMonthText.setText(String.valueOf(this.currentYear) 
				+ "年" + String.valueOf(this.currentMonth) + "月" );	    
	}		
初めに、引数でわたってきた数字とcurrentMonthで計算して、表示対象の年月を計算します。
で、現在表示している各テキストをいったんクリアーして、
CalendarInfoクラスを私用してカレンダー情報を再セットしています。
その際に、当時の日付は背景色を変えています。
例外処理などは全くしていませんが、一応今月のカレンダー表示は完成しました。
次は各ボタン(次の月、前の月)、各テキストをクリックしたときの動作を作ってい行きます。

今回はここまで

カテゴリーへ


CalenderMemoソフト作成(5)

更新を止めていたのですが、続きをぜひUPしてほしいとの要望が多々ありましたので
仕事がかなり多忙の中、重い腰を上げてUPします(笑)
今回は各ボタン(次の月、前の月)、各テキストをクリックしたときの動作を作ってい行きます。

まずは、全体のソースから

				
	@Override
	public void onClick(View v) {
		// TODO 自動生成されたメソッド・スタブ
		if(v.getId() == R.id.next_month_id) {
			this.SetCalendar(+1);
		}
		else if(v.getId() == R.id.previous_month_button) {
			this.SetCalendar(-1);			
		}
		else 
		{
			for(int i = 0 ; i < this.dayTextList.size(); i++) {
				if(this.dayTextList.get(i).getTextViewId() == v.getId()) {
					this.dayTextList.get(i).getTextObject().setBackgroundResource(R.drawable.text_selected_line);
					this.dayTextList.get(i).setSelected(true);
				}
				else {
					if(this.dayTextList.get(i).isNowDay() == true) {
						this.dayTextList.get(i).getTextObject().setBackgroundResource(R.drawable.text_now_line);
						this.dayTextList.get(i).setSelected(false);
					}
					else if(this.dayTextList.get(i).isSelected()) {
						this.dayTextList.get(i).getTextObject().setBackgroundResource(R.drawable.text_day_line);
						this.dayTextList.get(i).setSelected(false);
					}
				}
			}			
		}

	}

R.id.next_month_idは次の月ボタンのIDです。
次の月ボタンを押下された場合「this.SetCalendar(+1);」を呼んでます。
this.SetCalendar関数の引数に進めたい数字(月)を設定して呼ぶと
表示月が変更される仕組みです。

R.id.previous_month_buttonは前の月ボタンのIDです。
前の月ボタンを押下された場合「this.SetCalendar(-1);」を呼んでます。

this.dayTextList.get(i).getTextViewId()で、各日付テキストのIDを取得しています。
this.dayTextListをループしてIDを検索後「getTextObject()」テキストオブジェクトを取得します。
取得したオブジェクトに「setBackgroundResource」を使って背景色を変更しています。

これで一通りのカレンダー表示は出来るかと思います。
私はこれを使って社内で使用するタイムカードを作成しました。
他にも何か派生ソフトをつくれないか模索中です。
何かいいアイデア、またはこんなのを作りましたなどが有りましたらぜひ教えてください。

今回はここまで

カテゴリーへ


inserted by FC2 system