zoukankan      html  css  js  c++  java
  • android-对话框

    三种:Dialog、Dialog主题活动、Toast

    Dialog(Alert)

            Dialog d = new Dialog(MyActivity.this);
            //使用新窗口为它遮挡的窗口着色,并模糊它
            Window window = d.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
            
            //设置标题
            d.setTitle("Dialog Title");
            //扩充布局
            d.setContentView(R.layout.dialog_view);
            
            //找出布局中使用的TextView并设置其文本值
            TextView text = (TextView)d.findViewById(R.id.dialogTextView);
            text.setText("This is the text in my dialog");
    d.show();
            AlertDialog.Builder ad = new AlertDialog.Builder(context);
            ad.setTitle("title");
            ad.setMessage("message");
            ad.setPositiveButton("button1", new OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    
                }
            });
            ad.setNegativeButton("button2", new OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    
                }
            });
            ad.setCancelable(true);
            ad.setOnCancelListener(new OnCancelListener() {
                
                @Override
                public void onCancel(DialogInterface dialog) {        
                    
                }
            });
            ad.show();

    Dialog主题活动

    <activity android:name="MyDialogActivity" android:theme="@android:style/Theme.Dialog">
    </activity>

    Toast

    Toast.makeText(this, String.format("名字 %s,性别 %s,年龄 %d", data.getUserName(),data.getSex(),data.getAge()), Toast.LENGTH_SHORT).show();

    对话框的管理

    在activity中的OnCreateDialog和onPrepareDialog事件处理程序可以保存和管理对话框。在第一次到onCreateDialog中创建对话框之后,之后每一次调用showDialog都会触发onPrepareDialog,在这方法内可以修改所要显示的值

        static final private int TIME_DIALOG = 1;
        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case (TIME_DIALOG):
                AlertDialog.Builder timeDialog = new AlertDialog.Builder(this);
                timeDialog.setTitle("titile");
                timeDialog.setMessage("message");
                return timeDialog.create();
            }
            return null;
        }
        
        @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
            switch (id) {
            case (TIME_DIALOG):
                AlertDialog timeDialog = (AlertDialog) dialog;
                timeDialog.setMessage("new message");
                break;
            }
        }
        showDialog(TIME_DIALOG);
  • 相关阅读:
    vue中使用clipboard.js复制分本
    聊聊IOCP,聊聊异步编程
    dubbo RPC超时异常小结
    redis4.0 集群,jedis客户端连接配置
    Centos7 Zookeeper
    阿里云Centos 7.4 mssql-server
    Redis4.0 Cluster — Centos7
    Elasticsearch学习笔记 一
    Centos7安装ES 和 Docker搭建ES
    使用Docker快速创建.Net Core2.0 Nginx负载均衡节点
  • 原文地址:https://www.cnblogs.com/mingfung-liu/p/4514124.html
Copyright © 2011-2022 走看看