抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

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 Button mDialogCommit,mDialogCancel;
/**
* 秒
*/
private int mCount;

/**
* 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_num), mCount--);
mDialogCommit.setText(count);
if (mCount < 0) {
onCommit();
dismiss();
} 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;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
//设置宽度
attributes.width = 600;
attributes.height = WRAP_CONTENT;
//设置位置
// attributes.x=600;
// attributes.y=800;
window.setAttributes(attributes);

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

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

mDialogCommit.setOnClickListener(view -> onCommit());

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

mDialogCancel = (Button) mRootView.findViewById(R.id.dialog_cancel);
mDialogCancel.setOnClickListener(v -> {
onCancel();
dismiss();
});

this.setOnCancelListener(dialogInterface -> onCancel());

setContentView(mRootView);
}

/**
* 取消与退出时的公共操作
*/
private void onCancel() {
if (mCustomOnCancelListener != null) {
mCustomOnCancelListener.onCancel();
}
mHandler.removeCallbacksAndMessages(null);
}

private void onCommit() {
if (mCustomOnCommitListener != null) {
mCustomOnCommitListener.onCommit();
}
mHandler.removeCallbacksAndMessages(null);
dismiss();
}


//确定的回调

public interface CustomOnCommitListener {
/**
* 确定
*/
void onCommit();
}

private CustomOnCommitListener mCustomOnCommitListener;

public void setCustomOnCommitListener(CustomOnCommitListener customOnCommitListener) {
this.mCustomOnCommitListener = customOnCommitListener;
}

//取消的回调

public interface CustomOnCancelListener {
/**
* 退出
*/
void onCancel();
}

private CustomOnCancelListener mCustomOnCancelListener;

public void setCustomOnCancelListener(CustomOnCancelListener customOnCancelListener) {
this.mCustomOnCancelListener = customOnCancelListener;
}
}

String.xml

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

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

用的时候:

CountDownDialog dialog = new CountDownDialog(mContext, title, content, 10);
dialog.show();
dialog.setCustomOnCommitListener(new CountDownDialog.CustomOnCommitListener() {
@Override
public void onCommit() {
//TODO
Toast.makeText(mContext, "关机中", 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)


本站由以下主机服务商提供服务支持:

0条评论

发表评论