欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

安卓开发:四种对话框

程序员文章站 2022-06-30 19:15:12
先展示一下四种对话框的效果图: 普通对话框 单选对话框: 多选对话框: 进度条对话框: 一个简单的布局,放四个按钮: 四种对话框: ......

先展示一下四种对话框的效果图:

普通对话框

安卓开发:四种对话框

 

单选对话框:

安卓开发:四种对话框

 

多选对话框:

安卓开发:四种对话框

 

进度条对话框:

安卓开发:四种对话框

 

 

 

一个简单的布局,放四个按钮:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click1"
        android:text="普通对话框" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click2"
        android:text="单选对话框" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click3"
        android:text="多选对话框" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click4"
        android:text="进度条对话框" />

</LinearLayout>

 

 

四种对话框:

package org.dreamtech.dialog;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    // 普通对话框
    public void click1(View v) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("对话框标题");
        builder.setMessage("对话框内容");
        builder.setPositiveButton("确认", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "点击了确定按钮",
                        Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton("取消", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "点击了取消按钮",
                        Toast.LENGTH_LONG).show();
            }
        });
        builder.show();
    }

    // 单选对话框
    public void click2(View v) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("对话框标题");
        final String items[] = { "选项1", "选项2", "选项3" };
        builder.setSingleChoiceItems(items, -1, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String item = items[which];
                Toast.makeText(getApplicationContext(), "选中" + item,
                        Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    // 多选对话框
    public void click3(View v) {
        AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("对话框标题");
        final String items[] = { "选项1", "选项2", "选项3" };
        final boolean checkItems[] = { false, false, false };
        builder.setMultiChoiceItems(items, checkItems,
                new OnMultiChoiceClickListener() {

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

                    }
                });
        builder.setPositiveButton("确定", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < checkItems.length; i++) {
                    if (checkItems[i]) {
                        String choice = items[i];
                        sb.append(choice + " ");
                    }
                }
                Toast.makeText(getApplicationContext(), "选中" + sb.toString(),
                        Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        });
        builder.show();
    }

    // 进度条对话框
    public void click4(View v) {
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setTitle("正在加载");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
        new Thread() {
            public void run() {
                dialog.setMax(100);
                for (int i = 0; i <= 100; i++) {
                    dialog.setProgress(i);
                    SystemClock.sleep(50);
                }
                dialog.dismiss();
            };
        }.start();
    }
}