2012年10月7日日曜日

lesson14


********************  MemoActivity.java  ********************
package jp.lesson.study10;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MemoActivity extends Activity {
    // Loggerのタグ
    private static final String TAG = "Chapter04";
   
    // SharedPreferencesのインスタンス
    private SharedPreferences mPrefs;
   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        // SharedPreferencesのインスタンスを取得
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
       
        // SharedPreferencesから"content"の値を取得
        String content = mPrefs.getString("content", "");
       
        // 日付 * を整形出力するためのフォーマッターを生成
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd (E)");
       
        // 今日のカレンダーを取得
        Calendar cal = Calendar.getInstance();
       
        // 今日の日付を文字列に変換
        String date = fmt.format(cal.getTime());
       
        TextView txtsubject = (TextView) findViewById(R.id.textView1);
        EditText txtcontent = (EditText) findViewById(R.id.editText1);
       
        txtsubject.setText(date);
        txtcontent.setText(content);
    }
   
    public void onClickButton(View view) {
        switch(view.getId()){
            case R.id.button1:{
                AlertDialog.Builder dlg = new AlertDialog.Builder(this);
                dlg.setTitle(getString(R.string.lbSaveTitle));
                dlg.setPositiveButton(getString(R.string.lbYes),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO 自動生成されたメソッド・スタブ
                            // 保存処理
                            EditText txtcontent = (EditText) findViewById(R.id.editText1);
                            String content = txtcontent.getText().toString();
                           
                            // 保存されたデータを確認
                            Log.d(TAG, "content=" + content);
                           
                            // Editorのインスタンスを取得
                            Editor editor = mPrefs.edit();
                           
                            // "content"に入力された文字列を設定
                            editor.putString("content", content);
                           
                            // 設定を反映する
                            editor.commit();
                           
                            finish();
                        }
                    });
                dlg.setNegativeButton(getString(R.string.lbNo),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO 自動生成されたメソッド・スタブ
                            // キャンセル
                        }
                    });
                dlg.show();
            }break;
            case R.id.button2:{
                finish();
            }break;
        }
    }
}

********************  edit.xml  ********************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:text=""
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fillViewport="true" >
        <EditText
            android:gravity="top"
            android:text=""
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </EditText>
    </ScrollView>
    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/lbSave"
            android:onClick="onClickButton" />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/lbCancel"
            android:onClick="onClickButton" />
    </LinearLayout>
</LinearLayout>

********************  strings.xml  ********************
<resources>
    <string name="app_name">Study10</string>
    <string name="hello_world">hello</string>
    <string name="menu_settings">menu</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_memo">MemoActivity</string>
   
    <string name="lbSave">保存</string>
    <string name="lbCancel">キャンセル</string>
    <string name="lbSaveTitle">保存確認</string>
    <string name="lbYes">はい</string>
    <string name="lbNo">いいえ</string>
</resources>

********************  AndroidManifest.xml  ********************
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jp.lesson.study10"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MemoActivity"
            android:label="@string/title_memo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

0 件のコメント:

コメントを投稿