2017-09-26 66 views
1

我创建了自定义Progress Dialogue。重写属性如setCancelable(),setCanceledOnTouchOutside()不适用于我。Android自定义进度对话框 - setCancelable()

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); 

请问有人能解释一下吗?

+1

为什么设置“MetricsProgressDialogue”?而不是'CustomProgressDialogue'? –

+0

对不起,这是一个复制粘贴错误。立即修改 –

回答

1

,而不是覆盖可取消的方法创建一个静态方法,像这样的,然后传递所需的选项

这里是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; 
    } 
+1

这是一个很好的问题。现在只有我检查ProgressDialog Source code.Got it。:) –

1

你可以用这种方式尝试

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(); 

     } 

    } 
+1

那么,我的解决方案出了什么问题。是否有任何与上下文相关的问题 –

+0

@DonChakkappan可能是。我不确定 。你可以用这种方式。供参考。 '静态ProgressDialog'在这里创建枢轴角色。 –

+1

Umar的答案更适合。所以我会投票给你:) –

0

你卡恩添加

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); 

是工作...!

0

您的代码的问题在于,您正在覆盖CustomDialog类中的cancelable和OutsideTouchCancel,但您并未从ProgressDialog类中替换原始代码。所以当你在你的构造函数中调用super(context)时,它将启动你的对话框,其构建函数为Cancelable和OutsideTouchCancel。

所以在你CustomDialog的构造函数,你需要显式调用

dialog.setCancelable(false); 
dialog.setCanceledOnTouchOutside(false) 

你不需要覆盖这些功能的自定义类。