安卓常用属性三

筅森魡賤 提交于 2019-12-21 08:50:52

day 10

简单对话框

  String[] str = new String[]{"大数据", "物联网", "云计算", "无人机", "机器人",
            "人工智能", "5G", "3D打印", "区块链"};
    String str1 = "云计算";

 
//定义AlertDialog.Builder
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        //设置标题
        builder.setTitle("简单消息对话框");
        //设置显示内容
        builder.setMessage("大家好,这里是江洲老师云课堂!");
        //设置按钮,以及按钮对应的监听事件
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "您单击了【确定】", Toast.LENGTH_SHORT).show();

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "您单击了【取消】", Toast.LENGTH_SHORT).show();
            }
        });
        //显示对话框
        builder.show();

列表对话框

   AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("标题");
        //builder.setMessage("内容");
        //设置列表
        builder.setItems(str, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "您选择的是:" + str[i], Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();

单击对话框

 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("标题");
        //设置单选列表
        builder.setSingleChoiceItems(str, 2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                str1 = str[i];
            }
        });
//添加点击对话框点击确定按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, str1, Toast.LENGTH_SHORT).show();
            }
        });
//添加点击取消对话框
        builder.setNegativeButton("取消",new OnClickListener(){
            @Overide
            puublic void OnClick(DialogInterface arg0,int arg1){
                Toast.makeText(MainActivty.this,"你已经取消",Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();

多选列表对话框

String[] str1 = new String[]{"大数据", "物联网", "云计算", "无人机", "机器人",
            "人工智能", "5G", "3D打印", "区块链"};
    boolean[] flag = new boolean[]{true, false, false, false, false, false, false, false, false};
String content ="";
public void dialog1(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("多项选择对话框");
        //设置多选列表
        builder.setMultiChoiceItems(str1, flag, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                flag[i] = b;
            }
        });
        //设置按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
               //通过循环,判断哪几项被选中
                for (int j = 0; j < flag.length; j++) {
                    if (flag[j] == true) {
                        content += str1[j] + ";";
                    }
                }
                Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
                content = "";
            }
        });
        builder.show();
    }

自定义对话框

String[] title = new String[]{"保密设置", "安全", "系统设置", "网络", "我的文档", "导航服务",
            "我的音乐", "Email"};
    int[] images = new int[]{R.drawable.img01, R.drawable.img02,
            R.drawable.img03, R.drawable.img04, R.drawable.img05,
            R.drawable.img06, R.drawable.img07, R.drawable.img08};
    SimpleAdapter simpleAdapter;

public void dialog2(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("自定义对话框");
        //添加适配器:simpleAdapter
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        // 通过循环将title、images数组中的内容添加到List中
        for (int i = 0; i < title.length; i++) {
            // 添加String[]、int[]类型的数据
            Map<String, Object> map = new HashMap<String, Object>();
            // name、picture为键值,也可以说是代号
            map.put("name", title[i]);
            map.put("picture", images[i]);

            list.add(map);
        }

        simpleAdapter = new SimpleAdapter(MainActivity.this, list,
                R.layout.item, new String[]{"name", "picture"}, new int[]{
                R.id.textView1, R.id.imageView1});
        //添加数据(通过简单适配器完成)
        builder.setAdapter(simpleAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, title[i], Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();

    }

Notification 通知管理器

 manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //获取通知管理器
        NotificationChannel channel = new NotificationChannel(channelId, "simple", NotificationManager.IMPORTANCE_DEFAULT); //创建通知的通道
        manager.createNotificationChannel(channel);//为通知管理器添加通道
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Notification.Builder builder = new Notification.Builder(MainActivity.this, channelId); //定义 Notification.Builder
                builder.setSmallIcon(R.drawable.img); //设置小图标
                builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.img));//设置大图标
                builder.setTicker("通知来了"); //设置Ticker信息
                builder.setContentTitle(title.getText().toString()); //设置内容标题
                builder.setContentText(message.getText().toString());//设置内容文本
                builder.setDefaults(Notification.DEFAULT_ALL);
                manager.notify(0x01, builder.build()); //管理器启动通知
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                manager.cancelAll();//删除所有的通知
            }
        });

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!