我有一个首选项屏幕,其中包含自定义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
获取此布局中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);
}
}
答案 0 :(得分: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);
}