2013-02-14 51 views
0

如何在代碼中創建對話框首選項?以編程方式創建自定義DialogPreference

 MyDialog dialog=new MyDialog(getActivity()); 
     dialog.setTitle("asd"); 
     dialog.setKey("key"); 
     dialog.getDialog().show(); 

這是我的XML文件,並呼籲舞會偏好屏幕時,它工作正常

<!-- Custom Dialog --> 
    <com.pak.asd.MyDialog 
     android:key="key" android:title="title" 
     android:dialogTitle="title" 
     android:negativeButtonText="no" 
     android:positiveButtonText="yes" android:dialogMessage="message" /> 

這是我的課

public class MyDialog extends DialogPreference { 
    private Context c; 

    public MyDialog(Context c) 
    { 
    //is null correct ? 
     super(oContext,null);  
     this.oContext = oContext; 
    } 

    public MyDialog(Context c, AttributeSet attrs) 
    { 
     super(c, attrs);   
     this.c = c; 
    } 

} 
+0

我看到現在爲API21添加了構造函數,但是如果我們在較老的平臺中調用'super(c,null)',它會拋出一個異常。發生什麼事了。 – Samuel 2016-10-28 01:36:45

回答

0

DialogPreference沒有DialogPreference (Context context)構造函數,那麼,爲什麼你定義了一個MyDialog(Context c)構造函數嗎?通常,你不需要那個構造函數,就是第二個。

我以前從未使用DialogPreference,但您應該能夠以與基本類相同的方式創建您的自定義類。

+0

如何在代碼中創建? ,你能舉個例子嗎? – Lukap 2013-02-14 12:26:21

+0

你有什麼嘗試?在Java代碼中尋找DialogPreference的用法,我從來沒有用過它。任何示例都適合您的擴展課程,請不要擔心。 – thelawnmowerman 2013-02-14 12:28:14

相關問題