ソフトバンク、『X06HT HTC Desire』発売開始

2010年4月27日より、ソフトバンクモバイル初となるAndroid搭載スマートフォン「HTC Desire SoftBank X06HT」が発売されました。

詳しくはhttp://www.htc.com/jp/product/x06ht/overview.html

MediaPlayer を使ってみた

android で mp3, midi などを再生するには
MediaPlayer を使います

そして drawable/raw を作ってその中にメディアファイルを格納します

その後, MediaPlayer.create(context, resid); で作成します

というわけで, MediaPlayer をいじってみた

サンプルプログラム
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MediaPlayerTest extends Activity 
                             implements OnClickListener {
  private MediaPlayer mediaPlayer = null;
  private Button button_Play;
  private Button button_Pause;
  private Button button_Stop;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.soundtest);
    
    button_Play = (Button) findViewById(R.id.Button01);
    button_Play.setOnClickListener(this);
    button_Pause = (Button) findViewById(R.id.Button02);
    button_Pause.setOnClickListener(this);  
    button_Stop = (Button) findViewById(R.id.Button03);
    button_Stop.setOnClickListener(this);  

    // メディアプレイヤーの作成
    mediaPlayer = MediaPlayer.create(this, R.raw.sample);
    
    // ループ再生の設定
    // mediaPlayer.setLooping(true);
  }
  @Override
  public void onClick(View v) {
    if (v == button_Play) {
      // 再生してなかったら
      if (!mediaPlayer.isPlaying()) {
        // MediaPlayerの再生
        mediaPlayer.start();
      }
    } else if (v == button_Pause) {
      // MediaPlayerの一時停止
      mediaPlayer.pause();
    }
    else if (v == button_Stop) {
      // 再生してたら
      if (mediaPlayer.isPlaying()) {
        // MediaPlayerの停止
        mediaPlayer.stop();
        try {
          // MediaPlayerの準備
          mediaPlayer.prepare();
        } catch (Exception e) {}
      }
    }
  }
}

こんな感じ!

参考サイト
http://developer.android.com/intl/ja/reference/android/media/MediaPlayer.html

ToneGenerator を使ってみた

ToneGeneratorを使うと簡単にトーンを鳴らせます
コンストラクタは
ToneGenerator (int streamType, int volume);
第1引数はストリームの種類
第2引数は音量

んで、
startTone(int ToneType) or startTone(int ToneType, int durationMs)
で再生します
後者は再生する時間を指定
Toneの種類に関してはToneGeneratorから

というわけで, ToneGenarator をいじってみた

サンプルプログラム
import android.app.Activity;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ToneGeneratorTest extends Activity {
  ToneGenerator toneGenerator;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tonegeneratortest);

    // ToneGenerator の作成
    toneGenerator = new ToneGenerator(
        AudioManager.STREAM_ALARM,
        ToneGenerator.MAX_VOLUME
    );

    ((Button) findViewById(R.id.Button01))
    .setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        // Tone再生
        toneGenerator.startTone(ToneGenerator.TONE_CDMA_ABBR_ALERT);
        // toneGenarator.stratTone(ToneGenerator.TONE_CDMA_ABBR_ALERT, 1000);
      }
    });
    ((Button) findViewById(R.id.Button02))
    .setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        // Tone停止
        toneGenerator.stopTone();
      }
    });
  }
  
  @Override
  public void onDestroy() {
    super.onDestroy();
    
    // ToneGenerator の解放
    toneGenerator.release();
  }
}

こんな感じ!

参考サイト
http://developer.android.com/intl/ja/reference/android/media/ToneGenerator.html
http://developer.android.com/intl/ja/reference/android/media/AudioManager.html

Tab を作ってみた

Tab を使うには TabHost と TabWidget を使います
そして TabWidget でTabのコンテンツを表示するのに FrameLayout を使います

Tabのコンテンツとして 任意のViewを表示させるのと, Activityを起動する2種類があるようです

というわけで, Tab を作ってみた

注意するのは id として
  • TabHost は @android:id/tabhost
  • TabWidget は @android:id/tabs
  • メインの FrameLayout は @android:id/tabcotent
というのを指定してあげます

XMLリソース
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="5dp">
    <TabWidget 
      android:id="@android:id/tabs"
      ... />
    <FrameLayout 
      android:id="@android:id/tabcontent"
      android:padding="5dp"
      ... >
      <LinearLayout
        android:id="@+id/content1" 
        android:orientation="vertical"
        ... >
        <TextView 
          ... />
      </LinearLayout>
      <LinearLayout
        android:id="@+id/content2" 
            ・
            ・
            ・
      </LinearLayout>
    </FrameLayout>
  </LinearLayout>
</TabHost>

また, Tab が切り替わったときのアイコンの切り替えもXMLで指定できます
以下のように作ってres/drawable に置きます
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- 選択されたときの画像 -->
  <item android:drawable="@android:drawable/star_big_on"
        android:state_selected="true" />
  <!-- 選択されていないときの画像 -->
  <item android:drawable="@android:drawable/star_big_off" />
</selector>
んで, 本体
ここで Activity でなく TabActivity というのを継承させます

サンプルプログラム
import android.app.TabActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

public class TabTest extends TabActivity
                     implements OnTabChangeListener {
  private static final String TAG[] = {
    "tag1", "tag2", "tag3",
  };
  private static final String LABEL[] = {
    "Label1", "Label2", "Label3",
  };

  TabHost tabHost;
  TabHost.TabSpec spec;
  TextView textView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tabtest);
    textView = new TextView(this);
    
    // TabHost の取得
    tabHost = getTabHost();
    // Tab が切り替わったときに呼ばれるコールバックを登録
    tabHost.setOnTabChangedListener(this);

    /********** Tab その1 **********/
    // TabSpec の作成
    spec = tabHost.newTabSpec(TAG[0]);
    // インジケーターの設定
    spec.setIndicator(LABEL[0]);
    // Tab のコンテンツの設定
    spec.setContent(R.id.content1);
    // TabHost に Tab を追加
    tabHost.addTab(spec);

    /********** Tab その2 **********/
    spec = tabHost.newTabSpec(TAG[1])
    // アイコン付きインジケーターの設定
    .setIndicator(LABEL[1], getResources().getDrawable(R.drawable.icon))
    .setContent(R.id.content2);
    tabHost.addTab(spec);

    textView.setText("Text");
    textView.setBackgroundColor(Color.DKGRAY);
    textView.setTextColor(Color.RED);
    textView.setGravity(Gravity.CENTER);

    /********** Tab その3 **********/
    // 起動したいアクティビティのIntent作成
    Intent intent = new Intent().setClass(this, TabTest.class);
    
    spec = tabHost.newTabSpec(TAG[2])
    // インジケーターに任意のビューを設定
    .setIndicator(textView)
    // Intent を設定
    .setContent(intent);
    tabHost.addTab(spec);

    // 現在の Tab を設定
    tabHost.setCurrentTab(0);
  }
  // Tab が切り替わったときの動作 (引数はTag)
  public void onTabChanged(String tabId) {
    if (tabId == TAG[2])
      textView.setBackgroundColor(Color.LTGRAY);
    else
      textView.setBackgroundColor(Color.DKGRAY);
  }
}
これでOK!

プログラムを実行すると...


こんな感じ!

今回は Intent で自分自身を読んでます
当たり前ですが何回も呼ぶとオーバーフローして落ちます

Tab で TabActivity を呼べば多段の Tab ができそう!

参考サイト
http://developer.android.com/intl/ja/resources/tutorials/views/hello-tabwidget.html
http://developer.android.com/intl/ja/reference/android/widget/TabHost.html
http://developer.android.com/intl/ja/reference/android/widget/TabWidget.html

HorizontalScrollView を使ってみた

横方向にスクロールさせるには
HorizontalScrollView を使います
java.lang.Object
  ↳ android.view.View
    ↳ android.view.ViewGroup
      ↳ android.widget.FrameLayout
        ↳ android.widget.HorizontalScrollView

その名の通り ScrollView の横バージョンです

HorizontalScrollView は ScrollView と同様一つしか子ビューを持てないので
LineaLayout などにまとめて入れてやります

というわけで, HorizontalScrollView をいじってみた

XMLリソース
<HorizontalScrollView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:id="@+id/HorizontalScrollView">
  <LinearLayout 
    android:id="@+id/LinearLayout"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    <TextView 
      android:id="@+id/TextView" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:text="HorizontalScrollViewTest">
    </TextView>
        ・
        ・
        ・
  </LinearLayout>
</HorizontalScrollView>
アクティビティー側で setContentView で設定

プログラムを実行すると...


こんな感じ!

一番下にスクロールバーが表示されています

参考サイト
http://developer.android.com/intl/ja/reference/android/widget/HorizontalScrollView.html

XML で Menu

Menu を XML で作成したいときは
/res に menu というフォルダを作成し、その中にxmlファイルを作成します


XML は以下のようにすればOKです
submenu を作りたいときは item の中に menu を入れてあげます。

XML リソース
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:id="@+id/menu_item01" 
    android:title="Menu01"/>
  <group 
    android:id="@+id/menu_group01">
    <item 
      android:icon="@drawable/icon" 
      android:id="@+id/menu_item02" 
      android:title="Menu02"/>
  </group>
  <item 
    android:id="@+id/menu_item03" 
    android:title="Menu03">
    <menu>
      <item 
        android:id="@+id/submenu_item01" 
        android:title="Sub_Menu01"/>
      <item 
        android:id="@+id/submenu_item02" 
        android:title="Sub_Menu02/">
    </menu>
  </item>
</menu>

そして,
MenuInflater というクラスを使ってXMLを取得します

サンプルプログラム
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MenuXMLTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  // Option Menu が最初に表示される時に1度だけ呼び出される
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    // MenuInflater の取得
    MenuInflater menuInflater = getMenuInflater();
    // MenuInflater から XML の取得  
    menuInflater.inflate(R.menu.menutest, menu);
    return true;
  }
}

あとは普通に onOptionsItemSelected(MenuItem item) などをオーバーライドすればアイテムが選択された際の処理などもできます

プログラムを実行すると...

こんな感じ!

参考サイト
http://developer.android.com/intl/ja/guide/topics/resources/available-resources.html#menus
http://developer.android.com/intl/ja/reference/android/view/MenuInflater.html

Eclipse で Xperia用アプリ開発準備

EclipseでXperia用のアプリを開発するためには、

まず、以下のソニーエリクソンのページ(http://developer.sonyericsson.com/wportal/devworld/downloads)
から, Android の部分にある [All Android Downloads] をクリックし
『Sony Ericsson Xperia X10 add-on for the Android SDK』というのをダウンロード

ダウンロードしたファイルを解凍して [Android SDK]を保存してあるディレクトリを開いて、その中にある [add-ons]ディレクトリの中に置く

そしたら、次は Eclipse を開く

普通のAVDの作成と同じで
[Window] -> [Android SDK and AVD Manager] 移動して [New] をクリック
すると [Target] に『X10 (Sony Erricsson ...』というのがあるのでそれを選択

Skin も『X10』を選択(たぶんデフォルトでなってる)



んで [Create AVD]

そして、プロジェクトの作成

Android 1.6 用のものがあるならわざわざ新しいのを作らなくても大丈夫

こちらも [Target Name] のところに『X10』が現れるので選択



これでOK!

そして、[Run Configurations] で Tagetをさっき作った『X10』のAVDを選んで実行すると...


こんな感じ!

やっぱ Xperia かっこいいですね!

XML で Animation

Animation を XML で作成したいときは

/res に anim というフォルダを作成し、その中にxmlファイルを作成します

アニメーションの要素としては以下のようなものがあります
  • set
  • alpha
  • scale
  • translate
  • rotate
  • interpolator

サイズを指定するものでViewサイズに対する比率を指定できるわけですが
以下の2種類で指定します
%   : View のサイズに対する比率
%p  : 親View のサイズに対する比率

というわけで、アニメーションを作ってみた

下からズームインしてくるアニメーション
zoom_in_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <scale
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="50%"
    android:duration="1000">
  </scale>
  <translate
    android:fromYDelta="100%p"
    android:toYDelta="0.0"
    android:duration="1000">
  </translate>
</set>

こんな感じで作成します

作成したら
AnimationUtilsクラスの
loadAnimation(context, id) メソッドを使ってアニメーションを取得

例えば
AnimationUtils.loadAnimation(context, R.anim.zoom_in_from_bottom);
というふうにすれば取得できます!


参考サイト
http://developer.android.com/intl/ja/guide/topics/resources/available-resources.html#animation
http://developer.android.com/intl/ja/reference/android/view/animation/AnimationUtils.html

XML でカスタムView

既存の View を拡張して新たに自分だけの View を作成したいときは

まず、res/values に attrs.xml を作成します

attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <!-- name : custom view class name -->
  <declare-styleable name="CustomView">
    <attr name="text" format="string" />
    <attr name="color" format="color" />
    <attr name="size" format="dimension" />
  </declare-styleable>
</resources>

declare-styleable の name属性は作成するカスタムビューのクラス名を指定する

次に、レイアウトを作成します

xmlns:app="http://schemas.android.com/apk/res/パッケージの名前"
というのを追加します
app は勝手につけた名前

attrs.xml で作成した属性を指定します

XMLリソース
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res/androidtest.app"
  android:id="@+id/LinearLayout"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"> 
  <androidtest.app.CustomView
    android:id="@+id/CustomView" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" 
    app:text="CustomViewTest" 
    app:color="#FFFF0000"
    app:size="32sp" />
</LinearLayout>

そしたら、カスタムビューの作成

サンプルプログラム(CustomView)
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import androidtest.app.R;

public class CustomView extends View {
  String str;
  int color;
  int size;

  public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);

    // styleable から TypedArray の取得
    TypedArray tArray = 
      context.obtainStyledAttributes(
        attrs,
        R.styleable.CustomView
      );
  
    // TypedArray から String を取得
    str = tArray.getString(R.styleable.CustomView_text);
    // TypedArray から Color を取得
    color = tArray.getColor(R.styleable.CustomView_color, 0xFFFFFFFF);
    // TypedArray から Dimension を取得
    size = tArray.getDimensionPixelSize(R.styleable.CustomView_size, 12);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setColor(color);
    paint.setTextSize(size);
    canvas.drawText(str, 32, 64, paint);
  }
}

Activity では setContentView で作成した layout を指定するだけ

サンプルプログラム(Activity)
import android.app.Activity;
import android.os.Bundle;
import androidtest.app.R;

public class CustomViewTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.customviewtest);
  }
}

プログラムを実行すると...


こんな感じ!


参考サイト
http://developer.android.com/intl/ja/guide/topics/resources/available-resources.html#customresources

ToggleButton を使ってみた

ToggleButton は
java.lang.Object
  ↳ android.view.View
    ↳ android.widget.TextView
      ↳ android.widget.Button
        ↳ android.widget.CompoundButton
          ↳ android.widget.ToggleButton
を使います

というわけで、ToggleButton を使ってみた

サンプルプログラム
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.ToggleButton;

public class ToggleButtonTest extends Activity {
  ToggleButton toggleButton;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.togglebuttontest);
  
    toggleButton = (ToggleButton) findViewById(R.id.ToggleButton01);
  
    // ToggleButton が On のときのテキストを設定
    toggleButton.setTextOn("Toggle ON");
    // ToggleButton が Off のときのテキストを設定
    toggleButton.setTextOff("Toggle OFF");
  
    // ToggleButton が On かどうかを設定
    toggleButton.setChecked(true);
  
    // ToggleButton が On かどうかを取得
    boolean checked = toggleButton.isChecked(); 
  }
}

XMLリソースで設定もできます

XMLリソース
<ToggleButton
  android:id="@+id/ToggleButton"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:textOff="Toggle Off"
  android:textOn="Toggle On">
</ToggleButton>

プログラムを実行すると...



こんな感じ!

参考サイト
http://developer.android.com/intl/ja/reference/android/widget/ToggleButton.html

いろいろな AlertDialog

AlertDialog.Builderクラスを使っていろいろな AlertDialog を作ってみた

サンプルプログラム
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AlertDialogTest2 extends Activity implements View.OnClickListener {
  AlertDialog.Builder alertDialogBuilder;
  Button button[];

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alertdialogtest2);

    button = new Button[3];
    for (int i = 0; i < 3; i++) { d.Button01 + i);
      button[i].setOnClickListener(this);
    }
  }

  public void onClick(View v) {
    if (v == button[0]) {
      setEditAlertDialog();
    } else if (v == button[1]) {
      setSingleChoiceDialog();
    } else if (v == button[2]) {
       setMultiChoiceDialog();
    }
  }

  /* AlertDialog(EditText) */
  public void setEditAlertDialog() {
    EditText editText = new EditText(this);
    editText.setText("Please fill your name!");

    alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("EditTextDialog");

    // AlertDialog に View を設定
    alertDialogBuilder.setView(editText);

    // Positive Button を設定
    alertDialogBuilder.setPositiveButton(
      "Positive", 
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // Positive Button がクリックされた時の動作
        }
      }
    );
    alertDialogBuilder.show();
  }
 
  /* SingleChoiceDialog */
  public void setSingleChoiceDialog() {
    // 表示させるアイテム名
    final String[] str_items = {
      "One",
      "Two",
      "Three"
    };

    alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("SingleChoiceDialog");

    // 初めに選ばれているアイテム
    final int default_item = 0;

    // SingleChoiceDialog の作成
    alertDialogBuilder.setSingleChoiceItems(
      str_items, 
      default_item,
      new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // アイテムが選ばれたときの動作
        }
      }
    );
    // Positive Button を設定
    alertDialogBuilder.setPositiveButton(
      "Positive", 
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // Positive Button がクリックされた時の動作
        }
      }
    );
    alertDialogBuilder.show();
  }

  /* MultiChoiceDialog */
  public void setMultiChoiceDialog() {
    // 表示させるアイテム名
    final String[] str_items = {
      "Red",
      "Green",
      "Blue"
    };
    // 各アイテムがチェックされている状態かどうか
    final boolean[] flag_items = {
      false,
      true,
      false
    };

    alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("MultiChoiceDialog");

    // MultiChoiceDialog の作成
    alertDialogBuilder.setMultiChoiceItems(
      str_items, 
      flag_items,
      new OnMultiChoiceClickListener() {
        public void onClick(
            DialogInterface dialog, 
            int which, 
            boolean isChecked) {
          // アイテムが選ばれたときの動作
        }
      }
    );
    // Positive Button を設定
    alertDialogBuilder.setPositiveButton(
      "Positive", 
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // Positive Button がクリックされた時の動作
        }
      }
    );
    alertDialogBuilder.show();
  }
}
プログラムを実行すると...

AlertDialog(EditText)

SingleChoiceDialog

MultiChoiceDialog

こんな感じ!


参考サイト
http://developer.android.com/intl/ja/reference/android/app/AlertDialog.html
http://developer.android.com/intl/ja/reference/android/app/AlertDialog.Builder.html

Spannable で文字のマークアップ

Spannable はテキストをマークアップするためのインターフェースです

CharacterStyleなどのクラスを使って文字列の一部を
下線を引いたり、下付き文字にしたり、
上付き文字にしたり、URLを付けたり...
と様々なことができます

CharacterStyle のサブクラスには
  • UnderlineSpan
  • SubscriptSpan
  • SuperscriptSpan
  • URLSpan
  • ScaleXSpan
  • RelativeSizeSpan
  • etc...
といろいろあります

というわけで, Spannable でテキストをいじってみた

サンプルプログラム
import android.app.Activity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.Spannable.Factory;
import android.text.style.RelativeSizeSpan;
import android.text.style.ScaleXSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SpannableTest extends Activity {
  Spannable spannable;

  UnderlineSpan underline;
  SubscriptSpan subscript;
  SuperscriptSpan superscript;
  URLSpan url;
  ScaleXSpan scaleX;
  RelativeSizeSpan relative;

  TextView textView[];

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spannabletest);

    textView = new TextView[4];
    for (int i = 0; i < 4; i++) {
      textView[i] = (TextView) findViewById(R.id.TextView01 + i);
    }

    // UnderlineSpan
    underline = new UnderlineSpan();
    // SubscriptSpan
    subscript = new SubscriptSpan();
    // SuperscriptSpan
    superscript = new SuperscriptSpan();
    // URLSpan
    url = new URLSpan("http://weide-dev.blogspot.com/");
    // ScaleXSpan
    scaleX = new ScaleXSpan(0.5f);
    // RelativeSizeSpan
    relative = new RelativeSizeSpan(0.5f);

    // Factory の取得
    Factory factory = Spannable.Factory.getInstance();

    /* アンダーラインが引かれた文字列 */
    // Spannable の取得
    spannable = factory.newSpannable(textView[0].getText());
    // 0からテキストの長さ分まで下線を引く
    spannable.setSpan(
        underline,
        0,
        textView[0].getText().length(),
        spannable.getSpanFlags(underline)
    );
    // TextView にテキストを設定
    textView[0].setText(spannable, TextView.BufferType.SPANNABLE);

    /* 後ろ4文字が下付きの文字列 */
    spannable = factory.newSpannable(textView[1].getText());
    // 後ろ4文字を下付き文字にする
    spannable.setSpan(
        subscript,
        textView[1].getText().length() - 4,
        textView[1].getText().length(),
        spannable.getSpanFlags(subscript)
    );
    // 後ろ4文字のXのスケールを0.5倍する
    spannable.setSpan(
        scaleX,
        textView[1].getText().length() - 4,
        textView[1].getText().length(),
        spannable.getSpanFlags(scaleX)
    );
    textView[1].setText(spannable, TextView.BufferType.SPANNABLE);

    /* 後ろ4文字が上付きの文字列 */
    spannable = factory.newSpannable(textView[2].getText());
    // 後ろ4文字を上付き文字にする
    spannable.setSpan(
        superscript,
        textView[2].getText().length() - 4,
        textView[2].getText().length(),
        spannable.getSpanFlags(superscript)
    );
    // 後ろ4文字のサイズを0.5倍する
    spannable.setSpan(
        relative,
        textView[2].getText().length() - 4,
        textView[2].getText().length(),
        spannable.getSpanFlags(relative)
    );
    textView[2].setText(spannable, TextView.BufferType.SPANNABLE);

    /* URL文字列 */
    spannable = factory.newSpannable(textView[3].getText());
    // 0からテキストの長さ分までURLタイプにする
    spannable.setSpan(
        url,
        0,
        textView[3].getText().length(),
        spannable.getSpanFlags(url)
    );
    textView[3].setText(spannable, TextView.BufferType.SPANNABLE);
    textView[3].setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        // URLを開く
        url.onClick(textView[3]);
      }
    });
  }
}
プログラムを実行すると...


こんな感じ!


参考サイト
http://developer.android.com/intl/ja/reference/android/text/Spannable.html
http://developer.android.com/intl/ja/reference/android/text/style/CharacterStyle.html

Menu を使ってみた

Menu は
android.view.Menu
を使用します

さらに、Activity クラスの
  • public boolean onCreateOptionsMenu(Menu menu)
  • public boolean onPrepareOptionsMenu(Menu menu)
  • public boolean onOptionsItemSelected(MenuItem item)
  • public boolean onMenuOpened(int featureId, Menu menu)
などのメソッドを Override することで使用できます

というわけで、Menu をいじってみた

サンプルプログラム
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MenuTest extends Activity {
  private static final int MENU_ID1 = Menu.FIRST;
  private static final int MENU_ID2 = Menu.FIRST + 1;
  private static final int MENU_ID3 = Menu.FIRST + 2;
  private static final int SUBMENU_ID1 = Menu.FIRST + 21;
  private static final int SUBMENU_ID2 = Menu.FIRST + 22;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
  
    ((Button) findViewById(R.id.Button)).setOnClickListener(
      new OnClickListener() {
        public void onClick(View v) {
          // Menu の表示
          openOptionsMenu();
        }
      }
    );
  }

  // Option Menu が最初に表示される時に1度だけ呼び出される
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    boolean ret = super.onCreateOptionsMenu(menu);

    // Menu にアイテムを追加
    menu.add(Menu.NONE, MENU_ID1, Menu.NONE, "Menu1");
    // Menu に Icon 付きアイテムを追加
    menu.add(Menu.NONE, MENU_ID2, Menu.NONE, "Menu2").setIcon(R.drawable.icon);

    // SubMenu を追加
    SubMenu sMenu = menu.addSubMenu(Menu.NONE, MENU_ID3, 3, "Menu3");
    sMenu.add(Menu.NONE, SUBMENU_ID1, Menu.NONE, "SubMenu1");
    sMenu.add(Menu.NONE, SUBMENU_ID2, Menu.NONE, "SubMenu2");

    // Menu にショートカットキーを設定
    menu.findItem(MENU_ID1).setAlphabeticShortcut('a');
    menu.findItem(MENU_ID2).setAlphabeticShortcut('1');
    menu.findItem(MENU_ID3).setAlphabeticShortcut('s');

    return ret;
  }

  // Option Menu が表示される時の動作
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    return super.onPrepareOptionsMenu(menu);
  }

  // Option Menu のアイテムが選択された時の動作
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    boolean ret = super.onOptionsItemSelected(item);
    // 選ばれたアイテムの ID を取得
    switch(item.getItemId()) {
    case MENU_ID1:   // Menu1
      return true;
    case MENU_ID2:   // Menu2
      return true;
    case SUBMENU_ID1:   // Submenu1
      return true;
    default:
    }
    return ret;
  }

  // Option Menu が開かれた時の動作
  @Override
  public boolean onMenuOpened(int featureId, Menu menu) {
    return super.onMenuOpened(featureId, menu);
  }
}

プログラムを実行すると...


SubMenu は...

こんな感じ!

参考サイト
http://developer.android.com/intl/ja/reference/android/view/Menu.html
http://developer.android.com/intl/ja/reference/android/app/Activity.html

EditText を使ってみた

EditText は
java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.EditText
を使います

というわけで, EditText を使ってみた

サンプルプログラム
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;

public class EditTextTest extends Activity {
  EditText editText;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edittexttest);
  
    editText = (EditText) findViewById(R.id.EditText01);
  
    // EditText にテキストを設定
    editText.setText("EditTextTest");
  
    // EditText のインプットタイプを設定
    editText.setInputType(InputType.TYPE_CLASS_TEXT);
  
    // EditText の最大行の設定
    editText.setMaxLines(1);
  
    // EditText が空のときに表示させるヒントを設定
    editText.setHint("This is Hint");
  
    // EditText のカーソル位置を設定
    editText.setSelection(3);
    // EditText のテキストを全選択
    // editText.selectAll();
  
    // EditText のテキストを取得
    String str = editText.getText().toString();
  }
}

InputType には
  • 数字入力
  • アドレス入力
  • パスワード入力
  • オートコンプリート
  • 入力不可
  • etc...
といろいろあります
詳しくは InputTypeクラス を参照してください

XMLリソースで指定することもできます

XMLリソース
<EditText 
  android:hint="This is Hint"
  android:id="@+id/EditText" 
  android:inputType="text" 
  android:layout_height="wrap_content" 
  android:layout_width="wrap_content"  
  android:text="EditTextTest">
</EditText>

XMLリソースで
InputTypeを指定するときは android:InputType を使います

プログラムを実行すると...



こんな感じ!

また、ヒントは


こんな感じで表示されます!

参考サイト
http://developer.android.com/intl/ja/reference/android/widget/EditText.html

ScrollView を使ってみた

ScrollView を使うには
java.lang.Object
  ↳ android.view.View
    ↳ android.view.ViewGroup
      ↳ android.widget.FrameLayout
        ↳ android.widget.ScrollView
を使います

というわけで、ScrollView をいじってみた

サンプルプログラム
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class ScrollViewTest extends Activity {
  ScrollView scrollView;
  LinearLayout linearLayout;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    scrollView = new ScrollView(this);
    linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
  
    for (int i = 0; i < 12; i++) {
      Button button = new Button(this);
      button.setText("Button" + (i+1));
      linearLayout.addView(button);
    }

    // ScrollView に View を追加
    scrollView.addView(linearLayout);

    setContentView(scrollView);
  }
}

ScrollView は1つしか View を設定できないので LinearLayout などにまとめる必要があります XMLでまとめて設定することもできます XMLリソース
<ScrollView
  android:id="@+id/ScrollView"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent">
  <LinearLayout 
    android:id="@+id/LinearLayout"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <!-- 追加したいView -->
    <Button .../>
        ・
        ・
        ・
  </LinearLayout>
</ScrollView>

プログラムを実行すると...



こんな感じ!

右にスクロールバーが表示されて、上下に続きがあると少し暗くなっています

参考サイト
http://developer.android.com/intl/ja/reference/android/widget/ScrollView.html

SurfaceView を使ってみた

SurfaceView は
java.lang.Object
  ↳ android.view.View
    ↳ android.view.SurfaceView
を使用します

また、Surfaceの変更などを取得するために
SurfaceHolder.Callback
を使用します
このインターフェースは
  • public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
  • public void surfaceCreated(SurfaceHolder holder)
  • public void surfaceDestroyed(SurfaceHolder holder)
の3つのメソッドを実装する必要があります

というわけで、SurfaceView をいじってみた

サンプルプログラム
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SurfaceViewTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(new SurfaceTestView(this));
  }
 
  class SurfaceTestView extends SurfaceView 
        implements SurfaceHolder.Callback, Runnable {
    private Thread thread;
    private BitmapDrawable bitmapDrawable;
  
    public SurfaceTestView(Context context) {
      super(context);
   
      // SurfaceHolder の取得
      SurfaceHolder holder = getHolder();
   
      // SurfaceHolder に コールバックを設定
      holder.addCallback(this);
      holder.setFixedSize(getWidth(), getHeight());
   
      // フォーカスをあてる
      setFocusable(true);
   
      bitmapDrawable = new BitmapDrawable(
        context.getResources(),
        BitmapFactory.decodeResource(
          context.getResources(), 
          R.drawable.icon)
      );
    }

    // Surface が変更されたときの動作
    public void surfaceChanged(SurfaceHolder holder,
        int format, int width, int height) {
    }

    // Surface が作成されたときの動作
    public void surfaceCreated(SurfaceHolder holder) {
      thread = new Thread(this);
      thread.start();
    }

    // Surface が削除されたときの動作
    public void surfaceDestroyed(SurfaceHolder holder) {
      thread = null;
    }

    public void run() {
      while (thread != null) {
        // 描画の開始
        Canvas canvas = getHolder().lockCanvas();
    
        draw(canvas);
    
        // 描画の終了
        getHolder().unlockCanvasAndPost(canvas);
      }
    }

    @Override
    public void draw(Canvas canvas) {
      // 現在の状態を保存
      canvas.save();
   
      Paint paint = new Paint();
      paint.setColor(Color.RED);
      paint.setTextSize(32);
   
      bitmapDrawable.setBounds(0, 0, 96, 96);
      bitmapDrawable.draw(canvas);
      canvas.drawText("SurfaceViewTest", 0, 128, paint);

      paint.setAntiAlias(true);
      bitmapDrawable.setBounds(0, 160, 96, 256);
      bitmapDrawable.draw(canvas);
      canvas.drawText("SurfaceViewTest2", 0, 288, paint);

      // 現在の状態の変更
      canvas.restore();
    }
  }
}

プログラムを実行すると...


こんな感じ!(上:アンチエイリアスなし, 下:あり)

アンチエイリアスは時間がかかるが場合によってはかけた方が良さそう

参考サイト
http://developer.android.com/intl/ja/reference/android/view/SurfaceView.html
http://developer.android.com/intl/ja/reference/android/view/SurfaceHolder.Callback.html

Drawable を使ってみた

Drawableクラスは abstract class で
サブクラスには
  • BitmapDrawable
  • ColorDrawable
  • RotateDrawable
  • ScaleDrawable
  • etc...
といろいろあります

今回は BitmapDrawable クラスを使ってみた

BitmapDrawable は
java.lang.Object
  ↳ android.graphics.drawable.Drawable
    ↳ android.graphics.drawable.BitmapDrawable
を使用します

というわけで BitmapDrawable をいじってみた

サンプルプログラム
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

public class DrawableTest extends Activity {
  BitmapDrawable bitmapDrawable;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new DrawableTestView(this));
  }

  class DrawableTestView extends View {
    public DrawableTestView(Context context) {
      super(context);
      // BitmapDrawable を作成
      bitmapDrawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.icon);
    }
    @Override
    public void onDraw(Canvas canvas) {
      // BitmapDrawable の範囲を設定
      bitmapDrawable.setBounds(0, 0, 48, 48);
      // BitmapDrawable の描画
      bitmapDrawable.draw(canvas);
   
      // BitmapDrawable のアルファ値を設定
      bitmapDrawable.setAlpha(128);
      bitmapDrawable.setBounds(48, 0, 96, 48);
      bitmapDrawable.draw(canvas);
      bitmapDrawable.setAlpha(255);   

      // BitmapDrawable にアンチエイリアスを設定
      bitmapDrawable.setAntiAlias(true);
      bitmapDrawable.setBounds(0, 48, 240, 288);
      bitmapDrawable.draw(canvas);
   
      bitmapDrawable.setAntiAlias(false);
      bitmapDrawable.setBounds(240, 48, 480, 288);
      bitmapDrawable.draw(canvas);
   
      bitmapDrawable.setBounds(0, 0, getWidth(), getHeight());
      // BitmapDrawable にグラビティを設定
      bitmapDrawable.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
      bitmapDrawable.draw(canvas);
    }
  }
}

プログラムを実行してみると...


こんな感じ!(左:アンチエイリアスあり, 右:なし)

この画像だとアンチエイリアスの違いはよく分からないですね
状況によって使い分けるのが良さそう

参考サイト
http://developer.android.com/intl/ja/reference/android/graphics/drawable/Drawable.html
http://developer.android.com/intl/ja/reference/android/graphics/drawable/BitmapDrawable.html

Bitmap で Pixel 操作してみた

Bitmap をピクセル単位で操作するには

getPixel(int x, int y)
または
getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)
を使ってPixelを取得、

setPixel(int x, int y, int color)
または
setPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)
で設定します

というわけで、Pixel操作をしてみた

サンプルプログラム
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class BitmapTest2 extends Activity {
  Bitmap bitmap;
  int pixels[];
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new BitmapTest2View(this));
  }

  class BitmapTest2View extends View {
    public BitmapTest2View(Context context) {
      super(context);
   
      // リソースから Bitmap を取得 
      bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
   
      // もし編集不可なら、編集可能な Bitmap を複製
      if (!bitmap.isMutable()) {
        bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
      }
   
      int width = bitmap.getWidth();
      int height = bitmap.getHeight();
      pixels = new int[width * height];
   
      // Bitmap から Pixel を取得
      bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
   
      // Pixel 操作部分
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          int pixel = pixels[x + y * width];
     
          pixels[x + y * width] = Color.argb(
              Color.alpha(pixel), 
              Color.green(pixel), 
              Color.blue(pixel),
              Color.red(pixel)
          );
        }
      }
   
      // Bitmap に Pixel を設定
      bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    }
  
    @Override
    public void onDraw(Canvas canvas) {
      // Bitmap の描画
      canvas.drawBitmap(bitmap, 0, 0, new Paint());
    }
  }
}

プログラムを実行すると...


こんな感じ!

なんか紫色だと違和感が...

参考サイト
http://developer.android.com/intl/ja/reference/android/graphics/Bitmap.html

Fullscreen にしてみた

Window を フルスクリーンにするには

Windowのフラグに
WindowManager.LayoutParams.FLAG_FULLSCREEN
を設定します

また、タイトルバーを消すには
requestWindowFeature(Window.FEATURE_NO_TITLE);
を実行します

サンプルプログラム
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class FullScreenTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  
    // Window をフルスクリーンにする
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Title Bar を非表示にする
    requestWindowFeature(Window.FEATURE_NO_TITLE);
     
    setContentView(R.layout.main);
  }
}

setContextView より前に書かないとエラーになります

プログラムを実行すると...


こんな感じ!

Bitmap を使ってみた

Bitmap は
java.lang.Object
  ↳ android.graphics.Bitmap
を使用します

また外部ファイルやリソースを読み込むために
java.lang.Object
  ↳ android.graphics.BitmapFactory
を使用します

例えば drawable に入れてあるリソースを Bitmap として使うには
BitmapFactory クラスで変換して利用する
その後 Bitmap クラスを用いて大きさを変換させたり
回転させたり...

というわけで、Bitmap をいじってみた

サンプルプログラム
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class BitmapTest extends Activity {
  private static final int NUM = 4;
  Bitmap bitmap[];
  Matrix matrix;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new BitmapTestView(this));
  }

  class BitmapTestView extends View {
    public BitmapTestView(Context context) {
      super(context);
      bitmap = new Bitmap[NUM];

      // リソースから Bitmap を取得 
      bitmap[0] = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
      // Bitmap のサイズの取得
      int width = bitmap[0].getWidth();
      int height = bitmap[0].getHeight();
   
      // 45°回転させた Bitmap を作成
      matrix = new Matrix();
      matrix.postRotate(45.0f);  // 回転させる角度を指定
      bitmap[1] = Bitmap.createBitmap(bitmap[0], 0, 0, width, height, matrix, true);
   
      // Bitmap のスケールを指定して作成
      // この場合 (x * 0.5) × (y * 2) の大きさの画像になる
      matrix = new Matrix();
      matrix.postScale(0.5f, 2.0f);  // スケールを指定
      bitmap[2] = Bitmap.createBitmap(bitmap[0], 0, 0, width, height, matrix, true);
   
      // Bitmap の大きさを指定して作成
      // この場合 80 × 48 の大きさの画像になる
      bitmap[3] = Bitmap.createScaledBitmap(bitmap[0], 80, 48, true);
    }

    @Override
    public void onDraw(Canvas canvas) {
      Paint paint = new Paint();
      for (int i = 0; i < NUM; i++) {
        // Bitmap の描画
        canvas.drawBitmap(bitmap[i], i * 80, 0, paint);
      }
    }
  }
}

プログラムを実行すると...


こんな感じ!

ちなみに 引数の filter の部分(最後の引数)の値を変えて比較してみると...


こんな感じに! (左:true, 右:false)
filter をかけるとアンチエイリアスがかかっているっぽい

参考サイト
http://developer.android.com/intl/ja/reference/android/graphics/Bitmap.html
http://developer.android.com/intl/ja/reference/android/graphics/BitmapFactory.html

ソフトバンク、『X06HT HTC Desire』の予約開始


4月1日より、ソフトバンクモバイルのAndroid搭載スマートフォン「HTC Desire」の予約を開始しました

全国のソフトバンクモバイル携帯電話取り扱い店にて、1人1台まで受け付けるそうです

実質負担額は2万前後だとか

詳しくは公式サイトより

NTT docomo が 『Xperia (SO-01B)』を販売開始


4月1日より、NTT docomo から Google の Android OS を搭載したSony Ericsson製のスマートフォン『Xperia (SO-01B)』の販売を開始しました

詳しくは公式サイトより

スペシャルサイトがめっちゃかっこいいです

TimePickerDialog を使ってみた

TimePickerDialog は
java.lang.Object
  ↳ android.app.Dialog
    ↳ android.app.AlertDialog
      ↳ android.app.TimePickerDialog
を使います

というわけで、TimePickerDialog をいじってみた

サンプルプログラム
import java.util.Calendar;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimePickerDialogTest extends Activity {
  final Calendar calendar = Calendar.getInstance();
  // カレンダーから現在の '時' を取得
  int mHour = calendar.get(Calendar.HOUR_OF_DAY);
  // カレンダーから現在の '分' を取得
  int mMinute = calendar.get(Calendar.MINUTE);

  TimePickerDialog timePickerDialog;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    // TImePickerDialog の時刻が変更された時に呼び出されるコールバックを登録
    TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
      public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        mHour = hourOfDay; // '時' を取得
        mMinute = minute;  // '分' を取得
      }
    };

    // TimePickerDialog の作成
    timePickerDialog = new TimePickerDialog(
      this,     // 第1引数 : Context
      listener, // 第2引数 : TimePickerDialog.OnTimeSetListener
      mHour,    // 第3引数 : 時
      mMinute,  // 第4引数 : 分
      true      // 第5引数 : 24時間表示(true)かAM/PM表示(false)か
    );

    // Dialog の Positive Button を設定
    timePickerDialog.setButton(
      DialogInterface.BUTTON_POSITIVE,
      "Positive", 
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // Positive Button がクリックされた時の動作
        }
      }    
    );

    // Dialog の Negative Button を設定
    timePickerDialog.setButton(
      DialogInterface.BUTTON_NEGATIVE, 
      "Negative", 
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // Negative Button がクリックされた時の動作
        }
      }
    );
  
    // Dialog の Neutral Button を設定
    timePickerDialog.setButton(
      DialogInterface.BUTTON_NEUTRAL, 
      "Neutral", 
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // Neutral Button がクリックされた時の動作
        }
      }
    );

    // TimePickerDialog の表示
    timePickerDialog.show();
  }
}

プログラムを実行すると...


AM/PM表示のとき

こんな感じ!

ProgressDialog を使ってみた

ProgressDilaog は
java.lang.Object
  ↳ android.app.Dialog
    ↳ android.app.AlertDialog
      ↳ android.app.ProgressDialog
を使います

というわけで、ProgressDialog をいじってみた

サンプルプログラム
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

public class ProgressDialogTest extends Activity implements Runnable {
  ProgressDialog progressDialog;
  Thread thread;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progressdialogtest);

    progressDialog = new ProgressDialog(this);
  
    // ProgressDialog のタイトルを設定
    progressDialog.setTitle("Title");

    // ProgressDialog のメッセージを設定
    progressDialog.setMessage("Message");

    // ProgressDialog の確定(false)/不確定(true)を設定します
    progressDialog.setIndeterminate(false);

    // ProgressDialog のスタイルを水平スタイルに設定
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    // 円スタイルの場合
    // progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    // ProgressDialog の最大値を設定 (水平の時)
    progressDialog.setMax(100);

    // ProgressDialog の初期値を設定 (水平の時)
    progressDialog.incrementProgressBy(0);

    // ProgressDialog のセカンダリ値を設定 (水平の時)
    progressDialog.incrementSecondaryProgressBy(50);

    // ProgressDialog のキャンセルが可能かどうか
    progressDialog.setCancelable(false);
 
    // ProgressDialog のキャンセルされた時に呼び出されるコールバックを登録
    progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
      public void onCancel(DialogInterface dialog) {
        // Thread を停止
        stop();
      }
    });

    // Start ボタン
    findViewById(R.id.ProgressDialogTest_Button).setOnClickListener(  
      new View.OnClickListener() {  
        public void onClick(View view) {  
          // ProgressDialog を表示
          progressDialog.show();
          // Thread を起動
          init();
          start();
        }  
      }
    );

    // ProgressDialog の Cancel ボタン
    progressDialog.setButton(
      DialogInterface.BUTTON_NEGATIVE,
      "Cancel",
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // ProgressDialog をキャンセル
          dialog.cancel(); 
        }
      }
    );
  }
 
  /** Runnable のプログラム */
  public void init() {
    thread = null;
  }
  public void start() {
    if (thread == null) {
      thread = new Thread(this);
      thread.start();
    }
  }
  public void run() {
    int count = 0;
    Thread thisThread = Thread.currentThread();
    while (thisThread == thread) {
      // 100ms毎に Progress Bar を進める
      progressDialog.setProgress(count++);  
      try {
        thread.sleep(100);  
      } catch (InterruptedException e) {
      }
      if (count >= progressDialog.getMax()) {
        // Progress が完了
        break;
      }
    }
    // Progress Dialog を消す
    progressDialog.dismiss();
  }
  public void stop() {
    thread = null;
  }
}

プログラムを実行すると...

円スタイルのとき

こんな感じ!

DatePickerDialog を使ってみた

DatePickerDialog は
java.lang.Object
  ↳ android.app.Dialog
    ↳ android.app.AlertDialog
      ↳ android.app.DatePickerDialog
を使います

というわけで、DatePickerDialog をいじってみた

サンプルプログラム
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.DatePicker;

public class DatePickerDialogTest extends Activity {
  final Calendar calendar = Calendar.getInstance();
  // カレンダーから現在の '年' を取得
  int mYear = calendar.get(Calendar.YEAR);
  // カレンダーから現在の '月' を取得
  int mMonth = calendar.get(Calendar.MONTH);
  // カレンダーから現在の '日' を取得
  int mDay = calendar.get(Calendar.DAY_OF_MONTH);

  DatePickerDialog datePickerDialog;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // DatePickerDialog の日付が変更されたときに呼び出されるコールバックを登録
    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
      public void onDateSet(
          DatePicker view, 
          int year,
          int monthOfYear,
          int dayOfMonth) {                
        mYear = year;         // '年' を取得
        mMonth = monthOfYear; // '月' を取得
        mDay = dayOfMonth;    // '日' を取得
      }
    };

    // DatePickerDialog の作成
    datePickerDialog = new DatePickerDialog(
      this,     // 第1引数 : Context
      listener, // 第2引数 : DatePickerDialog.OnDateSetListener
      mYear,    // 第3引数 : 年
      mMonth,   // 第4引数 : 月
      mDay      // 第5引数 : 日
    );

    // Dialog の Positive Button を設定
    datePickerDialog.setButton(
      DialogInterface.BUTTON_POSITIVE,
        "Positive", 
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            // Positive Button がクリックされた時の動作
          }
        }    
    );

    // Dialog の Negative Button を設定
    datePickerDialog.setButton(
      DialogInterface.BUTTON_NEGATIVE, 
      "Negative", 
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // Negative Button がクリックされた時の動作
        }
      }
    );
  
    // Dialog の Neutral Button を設定
    datePickerDialog.setButton(
      DialogInterface.BUTTON_NEUTRAL, 
      "Neutral", 
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          // Neutral Button がクリックされた時の動作
        }
      }
    );

    // DatePickerDialog の表示
    datePickerDialog.show();
  }
}

プログラムを実行すると...


こんな感じ!