Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

Android Composite(Dialog)详解

$
0
0
目录:
    1.Dialog概述
    2.Dialog中的重要角色
    3.常见几种类型的Dialog简单使用
        3.1 提示型Dialog实现
        3.2 List型Dialog实现
        3.3 单选型Dialog实现
        3.4 多选型Dialog实现
        3.5 自定义Dialog
    
1.Dialog概述
    Dialog就是一个对用户操作进行反馈的弹框,通常在弹框中进行一些简单的提示或者简单选择。
    
2.Dialog中的重要角色
    2.1 AlertDialog类:
        AlertDialog即我们使用的窗口类,可以通过AlertDialog类的相关方法设置Dialog的属性以及显示,通常我们通过他的内部
    类Builder来构建他的实例,主要原因在于它的构造方法都是受保护的。
       
    2.2 AlertDialog.Builder类:
        AlertDialog.Builder即AlertDialog内的类,通过它来实例化Dialog,同时也可以通过他来设置AlertDialog的属性资源和
    Dialog的显示
    
3.常见几种类型的Dialog简单使用
    3.1 MainActivity.java
package com.example.dialog;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button simpleButton,listButton,singleButton,multiButton,customButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        simpleButton = (Button) findViewById(R.id.info_dg);
        listButton = (Button) findViewById(R.id.list_dg);
        singleButton = (Button) findViewById(R.id.single_dg);
        multiButton = (Button) findViewById(R.id.multi_dg);
        customButton = (Button) findViewById(R.id.custom_dg);
        //设置监听器
        simpleButton.setOnClickListener(this);
        listButton.setOnClickListener(this);
        singleButton.setOnClickListener(this);
        multiButton.setOnClickListener(this);
        customButton.setOnClickListener(this);

    }
    /*
* 弹出单选型的弹框
* */
    public void showMultiDialog(){
        //声明并初始化数据
        final CharSequence[] items = {"篮球","足球","游泳","唱歌"};
        final StringBuilder checkedStr = new StringBuilder();

        //声明并初始化AlertDialog.Builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置AlertDialog标题
        builder.setTitle("多选弹框标题");
        //设置图标
        builder.setIcon(R.drawable.a12);
        //设置数据
        builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked){
                    checkedStr.append(items[which].toString());
                }
            }
        });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,checkedStr,Toast.LENGTH_SHORT).show();
            }
        });
        //展示弹框
        builder.show();
    }
    /*
* 弹出单选型的弹框
* */
    public void showSingleDialog(){
        //声明并初始化数据
        final CharSequence[] items = {"男","女","不男不女"};

        //声明并初始化AlertDialog.Builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置AlertDialog标题
        builder.setTitle("单选弹框标题");
        //设置图标
        builder.setIcon(R.drawable.a12);
        //设置数据
        builder.setSingleChoiceItems(items,1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String selectItem = items[which].toString();
                Toast.makeText(MainActivity.this,selectItem,Toast.LENGTH_SHORT).show();

            }
        });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"确认",Toast.LENGTH_SHORT).show();
            }
        });
        //展示弹框
        builder.show();
    }
    /*
    * 弹出list型的弹框
    * */
    public void showListDialog(){
        //声明并初始化数据
        final CharSequence[] items = new CharSequence[5];
        for (int i =0;i<5;i++){
            items[i] ="数据"+i;
        }
        //声明并初始化AlertDialog.Builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置AlertDialog标题
        builder.setTitle("List型弹框标题");
        //设置图标
        builder.setIcon(R.drawable.a12);
        //设置数据并实现点击监听
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //获取选中项文本
                String selectItem = items[which].toString();
                Toast.makeText(MainActivity.this,selectItem,Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }
    /*
    * 弹出简单的弹框
    * */
    public void showSimpleDialog(){
        //声明并初始化AlertDialog.Builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置AlertDialog标题
        builder.setTitle("普通弹框标题");
        //设置图标
        builder.setIcon(R.drawable.a12);
        //设置提示信息
        builder.setMessage("普通弹框提示信息...");

        //设置确认按钮
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"确认",Toast.LENGTH_SHORT).show();
            }
        });
        //设置普通按钮
        builder.setNeutralButton("快点我", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"我就知道你要点我",Toast.LENGTH_SHORT).show();
            }
        });
        //设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }
    /*
    * 弹出自定义的弹框
    * */
    public void showCustomDialog(){
        final EditText userName,passWord;

        //声明并初始化布局加载器
        //LayoutInflater Inflater =getLayoutInflater();
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        //加载自定义布局
        View view = inflater.inflate(R.layout.custom_dialog,null);
        //初始化布局控件
        userName = (EditText) view.findViewById(R.id.username_et);
        passWord = (EditText) view.findViewById(R.id.pas_et);
        //声明并初始化AlertDialog.Builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置布局
        builder.setView(view);
        //设置登录按钮
        builder.setPositiveButton("登陆", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String nameStr = userName.getText().toString();
                String passStr = passWord.getText().toString();
                Toast.makeText(MainActivity.this,"用户名:"+nameStr+",密码:"+passStr,Toast.LENGTH_SHORT).show();
            }
        });
        //设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"取消登录",Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();

    }
    /*
    * 继承实现监听响应
    * */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.info_dg:
                showSimpleDialog();
                break;
            case R.id.list_dg:
                showListDialog();
                break;
            case R.id.single_dg:
                showSingleDialog();
                break;
            case R.id.multi_dg:
                showMultiDialog();
                break;
            case R.id.custom_dg:
                showCustomDialog();
                break;
        }
    }
} 


      
    3.2 主布局activity_main.xml
    
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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.example.dialog.MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提示型Dialog"
        android:id="@+id/info_dg"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List型Dialog"
        android:id="@+id/list_dg"
        android:layout_alignTop="@+id/info_dg"
        android:layout_toRightOf="@+id/info_dg"
        android:layout_toEndOf="@+id/info_dg" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单选型Dialog"
        android:id="@+id/single_dg"
        android:layout_alignTop="@+id/list_dg"
        android:layout_toRightOf="@+id/list_dg"
        android:layout_toEndOf="@+id/list_dg" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="多选型Dialog"
        android:id="@+id/multi_dg"
        android:layout_below="@+id/info_dg"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义Dialog"
        android:id="@+id/custom_dg"
        android:layout_alignTop="@+id/multi_dg"
        android:layout_toRightOf="@+id/multi_dg"
        android:layout_toEndOf="@+id/multi_dg" />
</RelativeLayout>    

  
    3.3 自定义布局custom_dialog.xml
 
<?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"
    android:padding="16dp"
    android:background="@color/colorAccent"
    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="登陆"
        android:id="@+id/title_login"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <RelativeLayout
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title_login"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="用户名:"
            android:id="@+id/username"
            android:layout_alignBaseline="@+id/username_et"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:hint="请输入用户名"
            android:ems="10"
            android:id="@+id/username_et"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/username"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/pas_et"
            android:hint="请输入密码"
            android:layout_below="@+id/username_et"
            android:layout_alignLeft="@+id/username_et"
            android:layout_alignStart="@+id/username_et"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="密码:"
            android:id="@+id/password"
            android:layout_alignBaseline="@+id/pas_et"
            android:layout_alignTop="@+id/pas_et"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    </RelativeLayout>

</RelativeLayout>

   
      
    3.4 效果截图(依次为:简单dialog,List型Dialog实现,单选型Dialog,多选型Dialog,自定义Dialog)
       

    

    
    推荐文章:
            http://blog.csdn.net/wangkeke1860/article/details/46497307
            http://blog.csdn.net/wangkeke1860/article/details/46488285
作者:qq_28057577 发表于2016/9/2 22:34:12 原文链接
阅读:55 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>