コード例

Android用語集

カテゴリー: AlertDialog.Builderクラス  閲覧数:439 配信日:2014-02-04 12:57


例1


▼ /HelloAndroid/src/android/style/HelloAndroidActivity5.java
package android.style;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;

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

// ダイアログの生成と表示
       AlertDialog.Builder dlg =
        new AlertDialog.Builder(this);
       dlg.setTitle("sample"); // タイトル設定
       dlg.setMessage("アラートダイアログ"); // メッセージ設定
       dlg.setPositiveButton("OK", null); // ボタン設定
       dlg.show(); // ダイアログの生成と表示

}
}


▼/HelloAndroid/res/layout/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" >

   <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello" />

</LinearLayout>



例2


DatePickerDialogのカスタマイズとカレンダーの表示
・AlertDialog.Builderクラスを使って、DatePickerDialogと同じようなコンポーネントを組み立てる
・年と日付を選択するダイアログを表示し、選択されると、該当の月のカレンダーを表示
※DatePickerDialogの実体は、android.app.AlertDialogクラスに、ビューとしてandroid.widget.DatePickerを設定したもの

▼ /Date/src/android/style/DateActivity.java
package android.style;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

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

sampleUI2();

public void printString(String s) {
// ベースのレイアウト取得
LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
TextView tv = new TextView(this);
tv.setTypeface(Typeface.MONOSPACE); // 等幅フォントの指定
tv.setText(s);
parent.addView(tv); // ビューの追加
}

// 年と日を選択するダイアログの表示
public void sampleUI2() {

// AlertDialog.Builderを生成
AlertDialog.Builder builder = new AlertDialog.Builder(this);

// DatePickerを生成
final DatePicker datePicker = new DatePicker(this);

// ViewにDatePickerを設定
builder.setView(datePicker);

// タイトルを設定
builder.setTitle("年と月を選択してください");

   // ダイアログに表示されるボタンを設定。ボタンに表示される文字列と、ボタンがクリックされたときのイベントリスナーを指定。選択された年と日は、DatePickerクラスのgetYear、getMonthメソッドで取得
builder.setPositiveButton("カレンダー表示",
new DatePickerDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
printCalendar(datePicker.getYear(),
// カレンダーの表示
datePicker.getMonth());
}
});

// AlertDialogを生成
AlertDialog alertDialog = builder.create();

   // 日を選択するパーツを非表示にする。年と月だけを選択したいため(日の選択は不要)。個別に設定できないため、setVisibilityメソッドを使って日を選択するビューを非表示にしている。DatePickerクラスには、このビューを特定するメソッドが見当たらないので、getChildAtメソッドで、3番目を指定(年、月、日とパーツが並ぶために、日にあたるgetChildAt(2))。このパーツの並びは、SimpleDateFormatクラスのtoPatternメソッドで得られるデフォルトのパターンに従っている。そのため、例えば米国のロケールであれば、パターンは"M/d/yy"となり、日は2番目となる。つまりgetChildAt(1)とする必要がある。
ViewGroup datePickerGroup = (ViewGroup) datePicker.getChildAt(0);
datePickerGroup.getChildAt(2).setVisibility(View.GONE);

// AlertDialogを表示
alertDialog.show();
}

// カレンダーの表示
public void printCalendar(int y, int m) {

printString(y + "年" + (m + 1) + "月のカレンダー");

   // 指定の月の初日を設定する
Calendar cal = Calendar.getInstance();
cal.set(y, m, 1);

   // 2桁で日を表示する
SimpleDateFormat sdf = new SimpleDateFormat("dd ");

   // 指定月の最終日(28、30、31など)と、その月の初日の曜日を取得(1週目の表示のため)。
int max = cal.getActualMaximum(Calendar.DATE);
   // 月初の曜日の取得
int w = cal.get(Calendar.DAY_OF_WEEK);

String wstr = "";
   // 第1週の空白を出力する
for (int i = 0; i < w - 1; i++) {
wstr += "   ";
}
int last = 0;
do {
for (int i = 0; i < 8 - w; i++) {
wstr += sdf.format(cal.getTime());
           // 月の最終日か?
if (max == cal.get(Calendar.DATE)) {
last++;
break;
}
           // カレンダーの日の表示は、Calendarオブジェクトを1日ずつ進めて、その値を表示。Calendarクラスのaddメソッドを利用し、1日を加算
cal.add(Calendar.DAY_OF_MONTH, 1); // 1日加算
}
printString(wstr);
w = 1;
wstr = "";
} while (last == 0);
}
}
}


・xml … レイアウト
▼/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/parent"
   >
</LinearLayout>