3

我有一个包含自定义的首选项屏幕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属性将布局应用于此自定义对话框。TextView我想从我的这个布局中获得对 a 的引用,DialogPreference但我不知道从哪里得到它:

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

}
4

1 回答 1

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);
}
于 2013-08-06T07:57:30.187 回答