• 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);
  • 相关阅读:
    MySQL事件(定时任务)
    MySQL存储过程
    WebSocket小记
    Python计算给定日期位于当年第几周
    报错解决——Failed building wheel for kenlm
    计算机基础系列之压缩算法
    计算机基础系列之内存、磁盘、二进制
    计算机基础系列之CPU
    常用正则表达大全
    报错解决——TypeError: LoadLibrary() argument 1 must be str, not None
  • 原文地址:https://www.cnblogs.com/mingfung-liu/p/4514124.html
Copyright © 2020-2023  润新知