Java Android自定义进度对话框-setCancelable()
我已经创建了一个自定义的Java Android自定义进度对话框-setCancelable(),java,android,dialog,Java,Android,Dialog,我已经创建了一个自定义的进度对话框。覆盖像setCancelable(),setCanceledOnTouchOutside()这样的属性对我来说不起作用 public class CustomProgressDialogue extends ProgressDialog { private final Context context; public CustomProgressDialogue(Context context) { super(context
进度对话框
。覆盖像setCancelable()
,setCanceledOnTouchOutside()
这样的属性对我来说不起作用
public class CustomProgressDialogue extends ProgressDialog {
private final Context context;
public CustomProgressDialogue(Context context) {
super(context);
this.context = context;
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_view);
}
// Didn't worked
@Override
public void setCancelable(boolean flag) {
super.setCancelable(false);
}
// Didn't worked
@Override
public void setCanceledOnTouchOutside(boolean cancel) {
super.setCanceledOnTouchOutside(false);
}
}
但同时在实例创建成功后应用相同的属性
// Worked
progressDialogue = new CustomProgressDialogue(getContext());
progressDialogue.setCancelable(false);
progressDialogue.setCanceledOnTouchOutside(false);
你能不能请人解释一下 你可以用这种方法试试
public class CustomProgressDialogue extends ProgressDialog
{
public static ProgressDialog(Context context) { // This section create Main role .
CustomProgressDialogue dialog = new CustomProgressDialogue (context);
dialog.setCancelable(false); // Add this
return dialog;
}
public CustomProgressDialogue (Context context) {
super(context);
}
public CustomProgressDialogue (Context context, int theme) {
super(context, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_view);
}
@Override
public void show() {
super.show();
}
@Override
public void dismiss()
{
super.dismiss();
}
}
不要重写可取消的方法,而是创建一个这样的静态方法,然后将所需的选项传递给它 ProgressDialog类是如何做到这一点的:
public static ProgressDialog show(Context context, CharSequence title,
CharSequence message, boolean indeterminate,
boolean cancelable, OnCancelListener cancelListener) {
ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setIndeterminate(indeterminate);
dialog.setCancelable(cancelable);
dialog.setOnCancelListener(cancelListener);
dialog.show();
return dialog;
}
你可以加上
public static ProgressDialog(Context context) {
CustomProgressDialogue dialog = new CustomProgressDialogue (context);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
我的方法是
final AlertDialog alertD = new AlertDialog.Builder(context).create();
alertD.setCanceledOnTouchOutside(false);
是工作 代码的问题是您正在覆盖CustomDialog类中的cancelable和OutsideTouchCancel,但您没有替换ProgressDialog类中的原始代码。因此,当您在构造函数上调用super(context)时,它将使用其内置函数启动对话框,用于Cancelable和OutsideTouchCancel 因此,在CustomDialog的构造函数中,需要显式调用
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false)
您不需要在自定义类中重写这些函数。为什么要设置
MetricsProgressDialogue
?不是CustomProgressDialogue
?很抱歉,这是一个复制粘贴错误。现在更正。那么,我的解决方案中有什么问题。是否存在与Context@DonChakkappan可能是。我不确定。你可以用这种方式。仅供参考<代码>静态ProgressDialog在这里创建pivot角色。Umar的答案更合适。所以我会投票给你:)这是一个很好的答案。现在只有我检查ProgressDialog源代码。明白了。:)