Androidアプリ開発入門 -- ダイアログの実装方法

動作サンプルを確認しましょう

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/check_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="マルチセレクトダイアログ"
         />
    <Button
        android:id="@+id/loading_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ローディングダイアログ"
         />
    <Button
        android:id="@+id/progress_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="プログレス表示付きローディング"
         />

    <Button
        android:id="@+id/fragment_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="推奨されるダイアログ作成手法"/>
</LinearLayout>

DialogActivity.class

package com.example.dialogsample;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class DialogActivity extends AppCompatActivity implements View.OnClickListener{

    String[] options = {"Google","Apple","Microsoft"};
    boolean[] selections = new boolean[options.length];

    ProgressDialog progressDlg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupViews();
    }

    private void setupViews() {
        findViewById(R.id.check_button).setOnClickListener(this);
        findViewById(R.id.loading_button).setOnClickListener(this);
        findViewById(R.id.progress_button).setOnClickListener(this);
        findViewById(R.id.fragment_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.check_button:  //チェックボックスダイアログ
                showCustomDialog(1);
                break;
            case R.id.loading_button:  //プログレスダイアログ
                final ProgressDialog loadingDialog = ProgressDialog.show(this,"処理中","しばらくお待ちください...");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(5000);
                        loadingDialog.dismiss();
                    }
                }).start();
                break;
            case R.id.progress_button:  //プログレスバー付きダイアログ
                showCustomDialog(2);
                progressDlg.setProgress(0);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0;i <= 15;i++){
                            SystemClock.sleep(1000);
                            progressDlg.incrementProgressBy(100/15);
                        }
                        progressDlg.dismiss();
                    }
                }).start();
                break;
            case R.id.fragment_button: //Android 3.0以降で推奨されるDialogFragmentを使用
                SampleDialogFragment sampleDialog = new SampleDialogFragment();
                sampleDialog.show(getFragmentManager(), "SampleDialog");
                break;
        }
    }

    @Override
    protected Dialog onCreateDialog(int dialogId) {
        switch (dialogId){
            case 1:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setTitle("タイトル");
                builder.setPositiveButton("決定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getBaseContext(),"決定がクリックされました",Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("キャンセル", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getBaseContext(),"キャンセルがクリックされました",Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setMultiChoiceItems(options, selections, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        Toast.makeText(getBaseContext(),options[which] + (isChecked?"選択されました!":"解除されました!"),Toast.LENGTH_SHORT).show();
                    }
                });
                return builder.create();
            case 2:
                progressDlg = new ProgressDialog(this);
                progressDlg.setIcon(R.mipmap.ic_launcher);
                progressDlg.setTitle("ファイルダウンロード中...");
                progressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDlg.setButton(DialogInterface.BUTTON_POSITIVE, "決定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getBaseContext(),"決定がクリックされました",Toast.LENGTH_SHORT).show();
                    }
                });
                progressDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "中止", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getBaseContext(),"中止がクリックされました",Toast.LENGTH_SHORT).show();
                    }
                });
                return progressDlg;
        }
        return null;
    }
}

SampleDialogFragment

package com.example.fragment;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.example.dialogsample.R;


public class SampleDialogFragment extends DialogFragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //アクションバーを非表示

        return inflater.inflate(R.layout.dialog_layout,container);
    }
}

dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/name_label"
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:gravity="center_vertical"
        android:text="名前:" />

    <EditText
        android:id="@+id/name_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/name_label"
        android:imeOptions="actionDone"
        android:inputType="text" />

    <Button
        android:id="@+id/confirm_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/name_input"
        android:text="完了" />

</RelativeLayout>

学習記録として作成しました。間違いがあればご指摘いただければ幸いです。

タグ: Android ダイアログ AlertDialog ProgressDialog DialogFragment

7月11日 20:23 投稿