Android系统对话框

Android系统对话框

效果图

2个按钮的对话框

这里写图片描述

3个按钮的对话框

这里写图片描述

自定义View的对话框

这里写图片描述

单选对话框

这里写图片描述

多选对话框

这里写图片描述

列表框

这里写图片描述

Code

XML

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kongqw.dialogdemo.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button1"
        android:text="2个按钮的对话框" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button2"
        android:text="3个按钮的对话框" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button3"
        android:text="一个View" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button4"
        android:text="单选框" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button5"
        android:text="复选框" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button6"
        android:text="列表框" />
</LinearLayout>

主类

package com.kongqw.dialogdemo;

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

public class MainActivity extends AppCompatActivity {

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


    public void button1(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认退出吗?");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    public void button2(View view) {
        Dialog dialog = new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.btn_star)
                .setTitle("升级")
                .setMessage("发现新版本")
                .setPositiveButton("升级", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "升级", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("不升级", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "不升级", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "忽略", Toast.LENGTH_SHORT).show();
                    }
                })
                .create();
        dialog.show();
    }


    public void button3(View view) {
        new AlertDialog.Builder(this)
                .setTitle("请输入")
                .setIcon(android.R.drawable.ic_dialog_info)
                // .setView(new EditText(this))
                .setView(View.inflate(this, R.layout.bitmain_tab, null))
                .setPositiveButton("确定", null)
                .setNegativeButton("取消", null)
                .show();
    }

    public void button4(View view) {
        new AlertDialog.Builder(this)
                .setTitle("单选框")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setSingleChoiceItems(new String[]{"Android", "IOS"}, 0, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setNegativeButton("取消", null).show();
    }

    public void button5(View view) {
        new AlertDialog.Builder(this)
                .setTitle("复选框")
                .setMultiChoiceItems(new String[]{"Android", "IOS"}, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        Toast.makeText(MainActivity.this,"which = " + which + "   isChecked = " + isChecked,Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", null)
                .setNegativeButton("取消", null)
                .show();
    }

    public void button6(View view) {
        new AlertDialog.Builder(this)
                .setTitle("列表框")
                .setItems(new String[]{"Item1", "Item2"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"which = " + which,Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("确定", null)
                .show();
    }
}

自定义View

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="请输入内容1"/>

    <EditText
        android:id="@+id/editText2"
        android:layout_margin="10dp"
        android:layout_below="@id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入内容2"/>

</RelativeLayout>

posted on 2016-02-29 16:46  封起De日子  阅读(277)  评论(0编辑  收藏  举报

导航