■
プログラムで配置するのもいいけど、XMLで配置したほうが編集もしやすかったりするので用途に応じて私は利用しています。
今回は会員登録用の情報入力ダイアログを作成するとして、EditText、RadioGroup、DatePicker、Buttonを利用したサンプルを。
表示されるのは下のような感じ。
regist_dialog.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:text="プロフィール登録" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textSize="20sp" android:gravity="center" android:layout_marginBottom="10dp"> </TextView> <!-- ニックネーム --> <TextView android:text="ニックネーム(10文字まで)" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textSize="14sp" android:layout_marginLeft="20dp"> </TextView> <EditText android:id="@+id/edittext_nickname" android:inputType="text" android:maxLength="10" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:imeOptions="actionDone" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> <View android:layout_width="wrap_content" android:background="#DDFFFFFF" android:layout_height="1dp" android:layout_marginBottom="1dp"></View> <!-- 性別 --> <TextView android:text="性別" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textSize="14sp" android:layout_marginLeft="20dp"> </TextView> <RadioGroup android:id="@+id/radiogroup_sex" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="20dp"> <RadioButton android:id="@+id/radio_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男性"> </RadioButton> <RadioButton android:id="@+id/radio_female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="女性"> </RadioButton> </RadioGroup> <View android:layout_width="wrap_content" android:background="#DDFFFFFF" android:layout_height="1dp" android:layout_marginBottom="1dp"></View> <!-- 年齢 --> <TextView android:text="生年月日" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textSize="14sp" android:layout_marginLeft="20dp"> </TextView> <DatePicker android:id="@+id/datepicker_birthday" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center"> </DatePicker> <!-- 登録 --> <Button android:id="@+id/button_regist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="登録"> </Button> </LinearLayout>
AccountRegistDialogクラス
package com.yourcompany.sample; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.view.Window; import android.view.KeyEvent; import android.view.ViewGroup; import android.view.View; import android.view.LayoutInflater; import android.widget.LinearLayout; import android.widget.Button; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.content.Context; import java.util.Calendar; /*! * @brief AccountRegistDialog */ public class AccountRegistDialog extends Dialog { private static final LinearLayout.LayoutParams FILL = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); private Context m_context; private DialogListener m_listener; private Button m_btnRegist; private EditText m_editNickName; private RadioGroup m_radioSex; private DatePicker m_datePicker; /*! * @brief コンストラクタ * * @param activity Activity * @param listener リスナー */ public AccountRegistDialog(Activity activity, DialogListener listener) { super((Context)activity); m_context = (Context)activity; m_listener = listener; m_btnRegist = null; m_editNickName = null; m_radioSex = null; m_datePicker = null; } /*! * @brief 生成イベント */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); LayoutInflater factory = LayoutInflater.from(m_context); final View entryView = factory.inflate(R.layout.regist_dialog, null); // 登録ボタン. m_btnRegist = (Button)entryView.findViewById(R.id.button_regist); m_btnRegist.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { m_listener.onRegistSelected(); dismiss(); } }); // ニックネーム入力. m_editNickName = (EditText)entryView.findViewById(R.id.edittext_nickname); m_editNickName.setText(""); // ラジオボタングループ. m_radioSex = (RadioGroup)entryView.findViewById(R.id.radiogroup_sex); m_radioSex.check(R.id.radio_male); m_radioSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.radio_male) { // 男にチェック. } else if (checkedId == R.id.radio_female) { // 女にチェック. } } }); // 日付入力. m_datePicker = (DatePicker)entryView.findViewById(R.id.datepicker_birthday); m_datePicker.init(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() { public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { } }); addContentView(entryView, FILL); } /*! * @brief キー処理 */ public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { m_listener.onCancel(); dismiss(); return true; } return super.onKeyDown(keyCode, event); } /*! * @brief ダイアログ処理の結果を通知するためのインターフェース */ public static interface DialogListener { /*! * @brief 登録ボタン押下 */ public abstract void onRegistSelected(); /*! * @brief キャンセル */ public abstract void onCancel(); } }
使用例
public class SampleActivity eve extends Activity { // 省略. private void showRegistDialog() { AccountRegistDialog dialog = new AccountRegistDialog(this, new ResultListener()); dialog.show(); } public class ResultListener implements AccountRegistDialog.DialogListener { public void onRegistSelected() { // 登録を選択した際の処理. } public void onCancel() { // キャンセルした際の処理. } } }
便利ですけど、iOSのInterfaceBuilderに比べたら・・・