ラベル UI の投稿を表示しています。 すべての投稿を表示
ラベル UI の投稿を表示しています。 すべての投稿を表示

2012年5月8日火曜日

AlertDialogの使い方(基本)



■AlertDialog

AlertDialogは、コンストラクタがprivateになっており 直接 new 出来ないので注意!
AlertDialog.Builder経由で、new してやる必要がある

●通常のダイアログ表示
// ダイアログの表示
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("TEST");
dlg.setMessage("Hello, World!");
dlg.show();

●選択ダイアログの表示
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setIcon(R.drawable.icon);
dlg.setTitle("Select Dialog");
dlg.setMessage("選択してください");
dlg.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        /* YES 選択時の処理 */
    }
});
dlg.setNegativeButton("No", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
       /* NO 選択時の処理 */
   }
});
dlg.show();

他にも、リストや複数の選択肢、プログレスバーなどを扱うことができる

■参考アドレス
http://www.hakkaku.net/articles/20090924-581
http://wikiwiki.jp/android/?UI%A

2012年3月19日月曜日

Notificationのフラグによる動作の設定


Notificationクラスのflagフィールドに代入することで、下記設定が可能
フラグは OR で代入して、複数設定することも出来る。

Notification.FLAG_AUTO_CANCEL
 しばらくすると自動的に通知が消える。ポップアップでお知らせするために使える

Notification.FLAG_INSISTENT
 通知ウィンドウを開くか、キャンセルされるまで、音が鳴り続ける。かなりうざそう

Notification.FLAG_ONGOING_EVENT
 通常の通知ウィンドウの上位のバーの部分に表示する。常に常駐するアプリに使える。

Notification.FLAG_NO_CLEAR
 通知をクリアを押してもクリアできないようにする FLAG_ACTIVITY_NEW_TASK ?

2012年2月6日月曜日

ProgressBarの使い方


プログレスバー(進捗)の表示には、ProgressBarウィジェットを用いる。

スタイルの指定(XMLレイアウト)
android:progressBarStyle        通常の円
android:progressBarStyleHorizontal   水平方向のバー
android:progressBarStyleLarge      大きい円
android:progressBarStyleSmall      小さい円

StyleHorizontal 使用時に限り、進捗状況の表示が可能になる。

メソッド:
setMax() プログレスバーの最大値の指定。上限はint型の範囲。
setProgress() 現在の進捗の値を設定(濃い色で表示される)
setSecondaryProgress() セカンダリの値の指定(薄い色で表示される)

使用例)
ProgressBar progress = (ProgressBar) findViewById(プログレスバーWidgetのID);
progress.setMax(100);
progress.setProgress(20);
progress.setSecondaryProgress(70);

2012年1月29日日曜日

Notification

1.NotificationManager を取得する
 
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2.Notificationオブジェクト生成
 
Notification notification = new Notification(
android.R.drawable.btn_default,
"通知情報が届きました",
System.currentTimeMillis());

・Notificationクラス
→通知情報のコンテキスト
 サウンド、バイブレーション、LEDを用いてアラートする事も
 引数1 icon 一覧に表示するアイコンID
 引数2 text 表示するテキスト
 引数3 表示する時間

3, PendingIntentを用意
 
Intent intent = new Intent(Intent.ACTION_VIEW);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
・PendingIntent
Intentを、タイミングを指定して発行することができるIntent。
時間を指定したり、イベント発生時に発行する。

4.通知情報の設定
 
notification.setLatestEventInfo(
getApplicationContext(),
"アプリ名",
"通知情報の説明文",
contentIntent);

5.一旦、Notificationを削除
 
nm.cancel(id)
6.Manager経由でNotify発行
 
nm.notify(R.string.app_name, notification);

2011年12月19日月曜日

ステータスバーに進捗を表示する


1.OnCreate()と、SetContentView()の間にプログレスバーの許可を書く

// プログレスバー許可
requestWindowFeature(Window.FEATURE_PROGRESS);


2. SetContentView()後に、まずはプログレスバーをOFFにしておく

// プログレスバー表示の設定
setProgressBarVisibility(false);

3.以下のメソッドで、プログレスバーを操作

引数は、 0 - 10000 の範囲で進捗を通知する。
10000になるとプログレスバーが消える

setProgress();

以上!

2011年12月18日日曜日

オプションメニューの作り方


使い方は、Activityのメソッドをオーバーライドする方式なので簡単。

○説明
menu.addメソッドでメニューを追加する。
・2番目の引数のMENU_ID01にメニューIDを指定する。Menu.FIRSTから順番に指定していけばOK
・4番目の引数に、メニュー名を指定する 
 
@Override
// オプションメニュー生成時のイベント
public boolean onCreateOptionsMenu(Menu menu)
{
 // オプションメニューを生成、追加する
 MenuItem item = menu.add(Menu.NONE, MENU_ID01, Menu.NONE, "SampleMenu");
 // アイコンを設定する
 // android.R.drawable内にシステムデフォルトのアイコンが多数用意されている
 item.setIcon(android.R.drawable.ic_menu_preferences);
 // キーボードショートカット
 item.setAlphabeticShortcut('e');
 return super.onCreateOptionsMenu(menu);
}

@Override
// オプションアイテムクリック時のイベント
public boolean onOptionsItemSelected(MenuItem item)
{
 switch (item.getItemId())
    {
    case MENU_ID01:
     // メニューが選択されたときの処理
     break;
    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}

@Override
// オプションメニューが閉じられたときのイベント
public void onOptionsMenuClosed(Menu menu)
{
}