本日はボタン処理を解説します。

やっとアプリ開発っぽくなってきました。
先に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:gravity="center"
android:orientation="vertical" >

<Button android:text="TEST1"
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


android:idって指定がありますね。
javaからオブジェクト(今回ならボタン)を取得するのに必要なので、
必ず指定します。
(アプリ内でユニーク)

次にjavaのコード(ボタンの取得~設定)です。
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_toast);

Button btn = (Button)findViewById(R.id.test);
btn.setOnClickListener(new BtnExecute());
}


Button btn = (Button)findViewById(R.id.test);
上記のfindViewByIdでボタンを取得しています。
戻り値がObject型なので今回ならButtonにキャストしてあげます。
(同じ方法でButton以外の画面要素を何でも取得できます。)

btn.setOnClickListener(new BtnExecute());
見た感じで分かりそうですが、ボタン押下時に実行する処理を指定します。

ボタン押下処理クラスは以下

public class BtnExecute implements View.OnClickListener
{
@Override
public void onClick(View v)
{
Toast.makeText(v.getContext(), "BUTTON", Toast.LENGTH_LONG).show();
}
}


重要なのはView.OnClickListenerをimplementsしてるところです。
要は View.OnClickListenerを使っているクラスなら誰でもいい訳です。
Activityにimplementsを付けてあげてもいいですし、
フロントコントローラーちっくに全ボタン共通の入口を用意してもいいです。

View#getContextの存在を知らずに、コンストラクタにContextを渡してクラス内に保持などは止めましょう
(必要なら有りです。)

動作イメージです。
まずは押下前(昔は押下を"おした"の変換ミスだと思ってました。)


ボタン押下


ここまでの情報+αでジャンケンゲームくらい出来そうですね。







SharedPreferencesとは何者かから説明します。

DBやファイル管理せずにアプリが消されるまで値を保持できる機能です。
さっそく嘘つきました。
実は端末のアプリ管理で「データ削除」を実行されても消えます。


とはいえ、ちょっとしたデータ管理には最適です。

とりあえず、ソースコードです。
public class MainActivity extends Activity
{

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_vh);
this.showMsg();
}

private void showMsg(){

SharedPreferences spf = getSharedPreferences("TEST_SPF", MODE_PRIVATE);
SharedPreferences.Editor editor = null;
String msg = spf.getString("msg", null);

if(null == msg){
editor = spf.edit();
editor.putString("msg", "SPF_MESSAGE");
editor.commit();
msg = "msg is null";
}

Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}


該当メソッドは「showMsg」です。

この作りだとデータが保存されていれば、保存されているデータを表示
データが無ければ新規保存します。

細かい解説です。

SharedPreferences spf = getSharedPreferences("TEST_SPF", MODE_PRIVATE);
インスタンスの生成(取得)をしています。
第一引数がSharedPreferencesの名前(DBのTBL名のイメージ)
第二引数は以下の通り
MODE_PRIVATE 他のアプリからはアクセス不可
MODE_WORLD_READABLE 他のアプリからの読み込み可能
MODE_WORLD_WRITEABLE 他のアプリからの書き込み可能

spf.getString("msg", null);
保存している値を取得します。
第一引数は保存した値のキー(DBではカラム名)
第二引数は取得出来なかった時に代入する値
今回はnullになります。

編集する時は以下
SharedPreferences.Editor editor = spf.edit();
*SharedPreferencesのインスタンスがspfの変数名でセットされている場合

値の追加は以下
editor.putString("msg", "SPF_MESSAGE");
put○○(キー, 値);です。
一通り値をputしたら以下のcommit()を忘れずに呼びます。
editor.commit();

最後に動作イメージです。

初回起動/データ削除後初回起動



2回目以降起動









本日はLayout xmlの話に戻ってhintについて記載します。

EditTextにて何を入力する欄なのかを表示しておく事ができます。

まずは画面を表示




EditTextにhint1、hint2と記載されていますね。
それがhintです。

では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" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="TEST1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="hint1"/>

<Button android:text="TEST3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button android:text="TEST4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="hint2" />

<Button android:text="TEST6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>


android:hintと指定している部分が該当部分です。

これだけでユーザが何を入力すればいいのか分かりやすくなるので、
活用していきたいですね。