16
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NumberPickerダイアログのカスタマイズ

Last updated at Posted at 2013-04-17

##レイアウトファイルの作成
Dialogのレイアウトを作成します。

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="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="30dp"
        android:layout_gravity="center"
        android:textSize="18dp"
        android:text="開始日" />
    <NumberPicker
        android:id="@+id/numberPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

##Dialogの生成
Dialogを表示する所で、以下を定義

    private void showDialog() {
        class MainFragmentDialog extends DialogFragment {
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                LayoutInflater inflater = getActivity().getLayoutInflater();
                View view = inflater.inflate(R.layout.setting_dialog, null, false);

                NumberPicker np = (NumberPicker) view.findViewById(R.id.numberPicker);
                np.setMaxValue(31);
                np.setMinValue(1);

                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Number Picker");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        // OKクリック時の処理
                    }
                });
                builder.setNegativeButton("Cancel", null);
                builder.setView(view);
                return builder.create();
            }
        }

        // Dialogの表示
        MainFragmentDialog dialog = new MainFragmentDialog();
        dialog.show(getFragmentManager(), "span_setting_dialog");
    }

##備考
Android4.0以降の設定画面で見れる、DataUsageの様なNumberPickerが作成出来ます。
※APIレベルが11以上でないと、NumberPickerが使えません。
sample
https://github.com/rmizusawa/NumPickerOnDialog

16
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?