AlertDialog 简单使用方法

      AlertDialog对话框的应用很普及,下面我整理了一下 Dialog的简单应用  包括单选,多选,复选,自定义样式对话框等 

     所有需要注意的事项 都在注释里面标明了 这里就不啰嗦了 谈不上技术分享 激励自己坚持下去


布局文件代码如下 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="4dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:padding="4dp"
    android:text="DialogDemo"
    android:textSize="15sp"
    android:textColor="#f0f"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <Button
        android:id="@+id/dialog_bt1"
        android:layout_marginTop="15dp"
        android:text="确认对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/dialog_bt2"
        android:layout_marginTop="15dp"
        android:text="单选对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/dialog_bt3"
        android:layout_marginTop="15dp"
        android:text="多选按钮对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/dialog_bt4"
        android:layout_marginTop="15dp"
        android:text="列表对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/dialog_bt5"
        android:layout_marginTop="15dp"
        android:text="自定义布局样式的对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


自定义对话框布局如下所示

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <ImageView
            android:src="@mipmap/icon_dou"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>



MainActivity 代码如下所示。


package com.imooc.www.moocdemo;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/5/15.
 */
public class DialogAcitivity extends AppCompatActivity implements View.OnClickListener {

    private String sing_dialog [] = {"男","女","女博士","程序员"};//单选对话框数据源
    private String mult_dialog [] = {"逗你玩","项目经理","测试","美工","程序员"};//多选对话框数据源
    private String item_dialog [] = {"大领导","小领导","程序员"};//列表对话框数据源

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialogactivity);
        //调用初始化点击事件 界面整洁
        initEvent();
    }

    //初始化点击数件
    private void initEvent() {
        //这样写点击事件直观,但是比较乱代码 还是下面的方法比较好看 哈哈
        findViewById(R.id.dialog_bt1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog1();
            }
        });

        findViewById(R.id.dialog_bt2).setOnClickListener(this);
        findViewById(R.id.dialog_bt3).setOnClickListener(this);
        findViewById(R.id.dialog_bt4).setOnClickListener(this);
        findViewById(R.id.dialog_bt5).setOnClickListener(this);
    }

    private void showDialog1() {
        //一下都是见名知意 不做过多的解释
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认对话框");
        builder.setIcon(R.mipmap.ico_dw1);
        builder.setMessage("确认对话框内容");
        //这里注意一下Onclick的包不要导错
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "点击了确定按钮", Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "点击了取消按钮", Toast.LENGTH_LONG).show();
//                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();//创建Dialgo
        dialog.show();//显示Dialog
    }

    private void showDialog2(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("单选按钮对话框");
        builder.setIcon(R.mipmap.ico_dw1);
        builder.setSingleChoiceItems(sing_dialog, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String str = sing_dialog[which];
                Toast.makeText(getApplicationContext(),"您点击了"+str,Toast.LENGTH_LONG).show();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void showDialog3(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("多选按钮对话框");
        builder.setIcon(R.mipmap.icon_dou);
        builder.setMultiChoiceItems(mult_dialog, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(getApplicationContext(),"我喜欢做"+mult_dialog[which],Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(),"我不喜欢做"+mult_dialog[which],Toast.LENGTH_LONG).show();
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void showDialog4(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("部门列表对话框");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setItems(item_dialog, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(),"我不喜欢做"+item_dialog[which],Toast.LENGTH_LONG).show();

            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void showDialog5(){
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.dlaloglayout,null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view);
        builder.setTitle("则是个自定义布局");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(),"点击了自定义对话框的确认按钮",Toast.LENGTH_LONG).show();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_bt2:

                showDialog2();
//                Toast.makeText(getApplicationContext(),"电解了",Toast.LENGTH_LONG).show();
                break;

            case R.id.dialog_bt3:
                showDialog3();
                break;

            case R.id.dialog_bt4:
                showDialog4();
                break;
            case R.id.dialog_bt5:
                showDialog5();
                break;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值