现在有一个需求,就是切换分辨率的时候,将dialog的样式换成中移需要的,如上图,之前是系统原生的dialog,这个是自己定义的.现在有一个问题是15s以后这个dialog自动dismiss,但是现在的问题是dialog一直显示,并没有隐藏.自定义dialog代码如下:
public class ETHangUpDialog extends Dialog{
private static final String TAG = "ETHangUpDialog";
public ETHangUpDialog(Context context) {
super(context);
}
public ETHangUpDialog(Context context, int theme) {
super(context, theme);
}
@Override
public void show() {
// TODO Auto-generated method stub
super.show();
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.gravity= Gravity.CENTER_VERTICAL;
layoutParams.width= WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.height= WindowManager.LayoutParams.WRAP_CONTENT;
getWindow().getDecorView().setPadding(0, 0, 0, 0);
getWindow().setAttributes(layoutParams);
}
public static class Builder{
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String neutralButtonStr;
private String negativeButtonText;
private OnClickListener positiveButtonClickListener;
private OnClickListener mNeutralButtonClickListener;
private OnClickListener negativeButtonClickListener;
private OnKeyListener mOnKeyListener;
public Builder(Context context) {
this.context = context;
}
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
*
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
*
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
*
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set the positive button resource and it's listener
*
*/
public Builder setPositiveButton(int positiveButtonText,
OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
public Builder setPositiveButton(String positiveButtonText,
OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
public Builder setNeutralButton(int neutralButtonText,
OnClickListener listener)
{
this.neutralButtonStr = (String) context
.getText(neutralButtonText);
this.mNeutralButtonClickListener = listener;
return this;
}
public Builder setNeutralButton(String neutralButtonText,
OnClickListener listener)
{
this.neutralButtonStr = neutralButtonText;
this.mNeutralButtonClickListener = listener;
return this;
}
public Builder setNegativeButton(int negativeButtonText,
OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
public Builder setOnKeyListener(OnKeyListener listener){
this.mOnKeyListener = listener;
return this;
}
public Builder setNegativeButton(String negativeButtonText,
OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
public ETHangUpDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ETHangUpDialog dialog = new ETHangUpDialog(context,R.style.Dialog);
View layout = inflater.inflate(R.layout.et_pop_dialog, null);
dialog.addContentView(layout, new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT));
((TextView) layout.findViewById(R.id.et_hangup_title)).setText(title);
if (positiveButtonText != null) {
((Button) layout.findViewById(R.id.et_hangup_positiveButton))
.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(R.id.et_hangup_positiveButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.et_hangup_positiveButton).setVisibility(
View.GONE);
}
if (mOnKeyListener != null){
dialog.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
mOnKeyListener.onKey(dialog,keyCode,event);
return false;
}
});
}
if (neutralButtonStr != null) {
((Button) layout.findViewById(R.id.et_hangup_neutralButton))
.setText(neutralButtonStr);
if (mNeutralButtonClickListener != null)
{
((Button) layout.findViewById(R.id.et_hangup_neutralButton))
.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
mNeutralButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_NEUTRAL);
}
});
}
}
else
{
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.et_hangup_neutralButton).setVisibility(
View.GONE);
}
// set the cancel button
if (negativeButtonText != null) {
((Button) layout.findViewById(R.id.et_hangup_negativeButton))
.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
((Button) layout.findViewById(R.id.et_hangup_negativeButton))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.et_hangup_negativeButton).setVisibility(
View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(R.id.et_hangup_message)).setText(message);
}
dialog.setContentView(layout);
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Log.d(TAG,"dialog ondismiss");
Intent intent = new Intent(BroadcastUtil.BROADCAST_IS_SETTING_ADVANCED);
context.sendBroadcast(intent);
}
});
return dialog;
}
}
}
在ETSettingDisplayResolutionFragment中创建Dialog并保证对象唯一:
//判断mHangUpDialog 为空的时候才创建对象,防止重复创建对象.
if (mHangUpDialog == null){
ETHangUpDialog.Builder builder = new ETHangUpDialog.Builder(this.getActivity());
builder.setMessage(R.string.tv_output_mode_set_confirm_dialog);
builder.setTitle(R.string.tv_output_mode_dialog_title);
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mProgressHandler.removeMessages(0);
dialog.dismiss();
dealOutputSetResult(get_operation, Activity.RESULT_OK);// 2101408
}
});
builder.setNegativeButton(R.string.display_position_dialog_no,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mProgressHandler.removeMessages(0);
dialog.dismiss();
dealOutputSetResult(get_operation, Activity.RESULT_CANCELED);
}
});
builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
boolean showing = mHangUpDialog.isShowing();
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && mHangUpDialog.isShowing()) {
dialog.cancel();
mProgressHandler.removeMessages(0);
dealOutputSetResult(get_operation, Activity.RESULT_CANCELED);
return true;
}
return false;
}
});
mHangUpDialog = builder.create();
}
mHangUpDialog.show();
问题所在,之前是将ETHangUpDialog.Builder设置成了成员变量而不是HangUpDialog,让dialog消失的时候我调用的是mBuilder.create().dismiss(),这样的话其实每次都是创建了一个新的dialog( final ETHangUpDialog dialog = new ETHangUpDialog(context,R.style.Dialog),导致调用dismiss方法的时候没有生效.