1. 程式人生 > >Android 各種Dialog例子

Android 各種Dialog例子

包括普通對話方塊、列表對話方塊、單選對話方塊、多選對話方塊、可編輯對話方塊。

Java:

package com.example.admin.dialog_1;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AlertDialog.Builder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import static com.example.admin.dialog_1.R.layout.editdialog;

public class MainActivity extends AppCompatActivity {

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

		//獲取xml上的幾個button
        Button onebtn = findViewById(R.id.onebtn);
        Button twobtn = findViewById(R.id.twobtn);
        Button threebtn = findViewById(R.id.threebtn);
        Button fourbtn = findViewById(R.id.fourbtn);
        Button fivebtn = findViewById(R.id.fivebtn);

        final Builder builder = new AlertDialog.Builder(MainActivity.this);
        onebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.setTitle("普通對話方塊");
                builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });               
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();
            }
        });

        twobtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Builder builder1 = new AlertDialog.Builder(MainActivity.this);
                builder1.setTitle("列表");
                builder1.setItems(new String[]{"計科1班", "計科2班", "計科3班", "計科4班", "計科5班", "計科6班"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder1.create().show();
            }
        });

        threebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Builder builder2 = new AlertDialog.Builder(MainActivity.this);
                builder2.setTitle("單選");
                builder2.setItems(new String[]{"男", "女", "其他"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder2.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder2.create().show();
            }
        });

        final boolean[] checkstatus = new boolean[]{true,false,true,false};
        fourbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Builder builder3 = new AlertDialog.Builder(MainActivity.this);
                builder3.setTitle("多選");
                builder3.setMultiChoiceItems(new String[]{"學習", "運動", "攝影", "旅行"}, checkstatus, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                    }
                });
                builder3.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder3.create().show();
            }
        });


        fivebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Builder builder4 = new AlertDialog.Builder(MainActivity.this);
                View view = getLayoutInflater().inflate(R.layout.editdialog,null);
                builder4.setTitle("可編輯");
                builder4.setView(view);
                builder4.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder4.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder4.create().show();
            }
        });
    }
}

Xml:

<?xml version="1.0" encoding="utf-8"?>
<Button
    android:id="@+id/onebtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="普通對話方塊"/>

<Button
    android:id="@+id/twobtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="列表對話方塊"/>

<Button
    android:id="@+id/threebtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="單選列表對話方塊"/>

<Button
    android:id="@+id/fourbtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="多選列表對話方塊"/>

<Button
    android:id="@+id/fivebtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="可編輯輸入對話方塊"/>

Flater xml:

<?xml version="1.0" encoding="utf-8"?>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />