Android自定义倒计时AlertDialog不成文记录


错误一:requestFeature() must be called before adding content

解决:setContentView(view)的执行需要在dialog.show()执行之后

简单自定义:

public class CountDownDialog extends AlertDialog {

    /**
     * 上下文
     */    private Context mContext;
    /**
     * 布局文件
     */    private View mRootView;

    /**
     * 提示框标题
     */    private String mTitle;
    /**
     * 提示框内容
     */    private String mContent;
    /**
     * 提交/确定 按钮 这里不使用button 没有必要
     */    private TextView mDialogCommit;
    /**
     * 秒
     */    private int mCount;
    /**
     * 回调的方法
     */    private View.OnClickListener mOnClickListener;

    /**
     * handler 标志位
     */    private static final  int DEFAULT_MSG=0;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DEFAULT_MSG:
                    String count = String.format(mContext.getString(R.string.dialog_yes), mCount);
                    mDialogCommit.setText(count);
                    mCount--;
                    if (mCount < 0) {
                        mOnClickListener.onClick(mRootView);
                        mHandler.removeCallbacksAndMessages(null);
                    }else {
                        sendEmptyMessageDelayed(DEFAULT_MSG, 1000);
                    }break;
                default:
                    break;
            }


        }
    };

    public CountDownDialog(Context context, String title, String content, int countDown) {
        super(context);
        this.mContext = context;
        mTitle = title;
        mContent = content;
        mCount = countDown;
        initView();
    }

    private void initView() {

        mRootView = LayoutInflater.from(mContext).inflate(R.layout.dialog_countdown, null);

        TextView dialogTitle = mRootView.findViewById(R.id.dialog_title);
        dialogTitle.setText(mTitle);
        TextView textContent = mRootView.findViewById(R.id.text_content);
        if (mContent != null && !mContent.isEmpty()) {
            textContent.setText(mContent);
        } else {
            textContent.setVisibility(View.GONE);
        }

        mDialogCommit = mRootView.findViewById(R.id.dialog_commit);
        String count = String.format(mContext.getString(R.string.dialog_yes), mCount--);
        mDialogCommit.setText(count);

        TextView dialogCancel = mRootView.findViewById(R.id.dialog_cancel);
        dialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        if (mCount != 0 && mCount > 0) {
            mHandler.sendEmptyMessageDelayed(DEFAULT_MSG, 1000);
        }
    }


    public void setOnCommitListener(View.OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
        mDialogCommit.setOnClickListener(onClickListener);
    }

    public void bindView() {
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        //设置宽度
        attributes.width = 600;
        attributes.height = WRAP_CONTENT;
        //设置位置
//        attributes.x=600;
//        attributes.y=800;
        window.setAttributes(attributes);

        setContentView(mRootView);
    }
}

String.xml

<string name="dialog_yes">是(%1$d)</string>

布局就不上了,自行设计即可。

自定义位置,窗口大小必须在dialog.show()后执行。所以用的时候:

final CountDownDialog dialog = new CountDownDialog(this, getString(R.string.temperature_title), getString(R.string.temperature_content), 10);
dialog.show();
dialog.bindView();
dialog.setOnCommitListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //TODO
        Toast.makeText(MainActivity.this, "关机中", Toast.LENGTH_SHORT).show();
        dialog.dismiss();
    }
});

总结:

设置宽高:

 Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        //设置宽度
        attributes.width = 600;
        attributes.height = WRAP_CONTENT;
        window.setAttributes(attributes);

设置位置:

 Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        //设置位置
        attributes.x=600;
        attributes.y=800;
        window.setAttributes(attributes);

宽高在xml中设置是不生效的,所以需要在代码中进行设置。

另外还有其他方式:如直接使用生成的对象来设置宽高:

dialog.getWindow().setLayout(400, 500)

 

声明:TIL|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA[ZH]协议进行授权

转载:转载请注明原文链接 - Android自定义倒计时AlertDialog不成文记录


Life is very interesting. In the end, some of your greatest pains become your greatest strengths.