2013-08-06 33 views
3

獲取參考佈局我有一個偏好屏幕,包含自定義DialogPreference從DialogPreference

<com.company.project.AboutDialog 
      android:key="about_dialog" 
      android:icon="@drawable/logo" 
      android:title="About Company Project" 
      android:summary="Version information" 
      android:dialogLayout="@layout/about_dialog" 
      android:negativeButtonText="" 
      /> 

正如你可以看到我現在用的是dialogLayout財產應用佈局,這個自定義對話框。我想從我的DialogPreference得到這個佈局中的TextView參考,但我不知道從哪裏得到它:

public class AboutDialog extends DialogPreference { 

    @Override 
    protected void onPrepareDialogBuilder(Builder builder) { 
     // Hide the title from the dialog 
     builder.setTitle(null); 
     super.onPrepareDialogBuilder(builder); 
       /* 
       * Where do I find a context for findViewById? 
       * ((TextView)findViewById(R.id.version)).setText("Hello, world"); 
       */ 
    } 

    public AboutDialog(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

} 

回答

2

後,一些谷歌搜索找到了它。我需要覆蓋onBindDialogView方法:

@Override 
protected void onBindDialogView(View view) { 
    String packageName = getContext().getPackageName(); 
    try { 
     ((TextView)view.findViewById(R.id.textView2)) 
       .setText(getContext().getPackageManager().getPackageInfo(packageName, 0).versionName); 
    } catch (PackageManager.NameNotFoundException e) { 
     Log.w("Project", "Couldn't find version name information for about dialog"); 
     e.printStackTrace(); 
    } 
    super.onBindDialogView(view); 
}