Androidはワンツーパンチ 三歩進んで二歩下がる

プログラミングやどうでもいい話

CharacterPickerDialogって知ってる?

Androidで有名なダイアログのクラスと言えば、AlertDialogクラスではないでしょうか?
またそのサブクラスのDatePickerDialog, ProgressDialog, TimePickerDialogあたりはご存知の方も多いと思います。
先日、日本Androidの会神戸支部で行われている「すからじお」( @scarviz さんが主催されています)というオンライン勉強会で始めてCharacterPickerDialogというクラスを知りました。
その時の参加者の方は始めて聞いたという方ばかりでした。
もちろん自分もです。

そこでちょっと調べてみました。
CharacterPickerDialog

Dialogクラスのサブクラス。 OnItemClickListener、OnClickListenerを実装。APIレベル1。
文字を選択できるダイアログを表示する。

パブリックメソッドはonClick,onItemClick
プロテクトメソッドはonClick
AlertDialogのようなsetTitle やsetMessageなどダイアログを修飾するメソッドは見当たりません。
CharacterPickerDialogを生成する時に引数で渡す文字列をダイアログ形式にして表示してくれるシンプルな構造のようです。

早速サンプルプログラムを作ってみました。
画面イメージはこんな感じです。



サンプルの構成は以下のようになります。
◯バージョンAndroid1.5
◯MyExampleCharacterPickerDialogActivity.java
画面上のボタンをクリックするとCharacterPickerDialogを表示します
 ダイアログのボタンがクリックされたら、内容をToastで表示します。
◯main.xml
 画面上に「CharacterPickerDialogを表示する」というボタンを表示しているだけのレイアウトです。
◯AndroidManifest.xml

◯MyExampleCharacterPickerDialogActivity.java

package com.sakurafish.example.myexamplecharacterpickerdialog;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.CharacterPickerDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;

public class MyExampleCharacterPickerDialogActivity extends Activity {
    private CharacterPickerDialog cpd = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) this.findViewById(R.id.Button01);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cpd.show();
            }
        });

        String options = "abcdefghijあいうえお";
        cpd = new CharacterPickerDialog(this, new View(this), null,
                options,
                false) {
            public void onClick(View v) {
                // onClickはキャンセルボタンの処理をします
                Toast.makeText(getApplicationContext(),
                        "onClick! " + ((Button) v).getText().toString(),
                        Toast.LENGTH_SHORT).show();
                dismiss();
            }

            public void onItemClick(AdapterView parent, View view, int position, long id)
            {
                // onItemClickは文字ボタンの処理をします
                String message = ("onItemClick! " + ((Button) view).getText().toString())
                        + " position=" +
                        position
                        + " id=" + id;
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                dismiss();
            }
        };
    }
}

◯main.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" >

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CharacterPickerDialogを表示する" />
</LinearLayout>

◯AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sakurafish.example.myexamplecharacterpickerdialog"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="3" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MyExampleCharacterPickerDialogActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>