dialog的实现

各式各样的dialog,都在这里了,大家可以写下来自己动手操作看看效果,别忘了添加图片哦。

首先MainActivity布局文件代码:

package com.example.third_five_dialog_android;

import android.app.Activity;
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.Toast;

public class MainActivity extends Activity {
    String[] sing_list = {"学习", "吃饭", "工作", "睡觉", "其他",};
    String[] mult_list = {"篮球", "足球", "羽毛球", "乒乓球", "跳跳球"};
    String[] item_list = {"清华大学", "北京大学", "北京科技大学", "中国航空航天大学", "华中科技大学"};

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

        initEvent1();

    }


    private void initEvent1() {
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog1();
            }
        });

        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog2();
            }
        });

        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog3();
            }
        });

        findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog4();
            }
        });

        findViewById(R.id.button5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog5();
            }
        });
    }

    //确认对话框
    private void showDialog1() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认对话框");//设置标题
        builder.setIcon(R.drawable.customize);//引用图片
        builder.setMessage("这是确认对话框提示内容");//设置提示框内容
        //确认按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "你点击了确认", Toast.LENGTH_LONG).show();
            }
        });
        //取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_LONG).show();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    //单选按钮对话框
    private void showDialog2() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择你正在做的事情");//设置标题
        builder.setIcon(R.drawable.customize);//引用图片
        builder.setSingleChoiceItems(sing_list, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String str = sing_list[i];
                Toast.makeText(MainActivity.this, "这个是" + 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.drawable.customize);//引用图片
        builder.setMultiChoiceItems(mult_list, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                if (b) {
                    Toast.makeText(MainActivity.this, "我喜欢" + mult_list[i] + "!", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "我不喜欢" + mult_list[i] + "!", Toast.LENGTH_LONG).show();
                }
            }
        });
        //点击取消会隐藏对话框
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    //显示列表对话框
    private void showDialog4() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择学校");//设置标题
        builder.setIcon(R.drawable.customize);//引用图片
        builder.setItems(item_list, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "我选择了" + item_list[i] + "!", Toast.LENGTH_LONG).show();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    //自定义对话框
    private void showDialog5() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.item_layout, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("自定义对话框");//设置标题
        builder.setIcon(R.drawable.customize);//设置图标
        builder.setView(view);//加入布局
        AlertDialog dialog = builder.create();
        dialog.show();//显示对话框
    }
}
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"
    tools:context="com.example.third_five_dialog_android.MainActivity">

    <Button
        android:text="确认对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1"/>
    <Button
        android:text="单选按钮对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"/>
    <Button
        android:text="单选按钮对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button3"/>
    <Button
        android:text="列表对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button4"/>
    <Button
        android:text="自定义对话框"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button5"/>


</LinearLayout>

自定义对话框xml布局文件,可以根据自己的喜好进行自定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:hint="请输入内容。。。"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="提交"/>
    </LinearLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/youshan"/>

</LinearLayout>




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值