android中Dialog的使用

1. Dialog概述

对话框是提示用户做出决定或输入更多信息的小窗口。对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模态框事件。

android开发网站上是这样写的https://developer.android.google.cn/guide/topics/ui/dialogs.html?hl=zh-cn#java
Dialog 类是对话框的基类,但您应避免直接实例化 Dialog,而是使用下列子类之一:

  • AlertDialog
    此对话框可显示标题、最多三个按钮、可选择项列表或自定义布局。
  • DatePickerDialog 或 TimePickerDialog
    此对话框带有允许用户选择日期或时间的预定义界面。

2. 项目结构

在这里插入图片描述
其中EditActive是我们用的Dialog类,对应的edit_active.xml是对应的布局样式文件。

3. Dialog的java实现

public class EditActive extends Dialog {

    private Context mContext;
    private MyActive myActive;
    private EditText edTime;
    private String unit;
    private TextWatcher mTextWatcher;
    private Boolean flag;
    private View myActiveView;


    public EditActive(Context context, MyActive myActive, View v) {
        super(context);
        this.mContext = context;
        this.myActive = myActive;
        this.unit = "mins";
        this.flag = false;
        this.myActiveView = v;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        initView();
        setListener();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.edit_active, null);
        setContentView(view);

        ImageView activeImage = findViewById(R.id.iv_active_image);
        TextView activeName = findViewById(R.id.tv_active_name);
        activeImage.setImageResource(myActive.getImageId());
        activeName.setText(myActive.getName());

        Button editActiveBtn = findViewById(R.id.bt_edit_active);
        edTime = findViewById(R.id.et_edit_time);

        editActiveBtn.setOnClickListener(v -> {
            String regEx="[a-zA-Z]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(edTime.getText().toString().trim());
            String temp = m.replaceAll("").trim();
            TextView activeTime = myActiveView.findViewById(R.id.active_time);
            LinearLayout activeBody = myActiveView.findViewById(R.id.active_body);
            if(temp.equals("")){
                myActive.setSelected(0);
                activeTime.setText("");
                activeBody.setBackgroundColor(Color.parseColor("#ffd988"));
            }
            else {
                myActive.setSelected(1);
                activeTime.setText(temp+" "+unit);
                activeBody.setBackgroundColor(Color.parseColor("#82c460"));
            }
        });

    }
    private void setListener(){

        mTextWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable editable) {
                if (flag){
                    flag = false;
                    return;
                }
                else {
                    flag = true;
                    String regEx="[a-zA-Z]";
                    Pattern p = Pattern.compile(regEx);
                    Matcher m = p.matcher(editable.toString().trim());
                    String temp = m.replaceAll("").trim();
                    if (temp.length()==0){
                        edTime.setText("");
                    }
                    else {
                        edTime.setText(temp+" "+unit);
                    }

                }
            }
        };
        edTime.addTextChangedListener(mTextWatcher);
    }

}
  • public EditActive(Context context, MyActive myActive, View v)
    初始化函数包含了在显示Dialog中所需要用的参数,其中Context context是必须的,它表示了这个Dialog加载中的上下文,我的理解就是这个Dialog是在什么页面上加载的。
  • protected void onCreate(Bundle savedInstanceState)
    创建的函数的必须的,这里我们包含了initView();setListener();这两个函数,其中initView用来加载我们在layout中写的自定义页面样式文件和Dialog页面中按钮点击事件的函数,setListener函数反映我们Dialog页面中EditView中输入改变的时候对应的动作。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值