自定义 EditTextPreference 不显示 EditText 区域
Custom EditTextPreference doesn't show EditText area
我的设置 Activity 包含来自 R.xml.preferences 的首选项列表。这里的首选项之一如下所示:
<AuthPreference
android:title="@string/auth_title"
android:summary="@string/auth_summary"
android:key="pass"
android:defaultValue="1"
android:dialogMessage="Provide a message"
android:layout="@layout/preference_auth"/>
此首选项扩展了 EditTextPreference class 所以我希望它在单击首选项后显示 EditText 区域。但是只有空对话框,只有 no 和 yes 按钮。代码如下:
public class AuthPreference extends EditTextPreference {
private ImageView icon;
private EditText et;
public AuthPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setNegativeButtonText("no");
setPositiveButtonText("yes");
}
public AuthPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setNegativeButtonText("no");
setPositiveButtonText("yes");
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
icon = (ImageView) view.findViewById(R.id.iconSelected);
}
@Override
protected void onBindDialogView(View view) {
some code
}
}
怎么了?
事实证明,第二个构造函数是错误的,因为通过设置为 0 的 super 将第三个参数传递给了直接父级。所以在更改之后我有
super(context, attrs);
而不是
super(context, attrs, 0);
您只需要小心并确保将相应的参数传递给特定父级的构造函数。
我的设置 Activity 包含来自 R.xml.preferences 的首选项列表。这里的首选项之一如下所示:
<AuthPreference
android:title="@string/auth_title"
android:summary="@string/auth_summary"
android:key="pass"
android:defaultValue="1"
android:dialogMessage="Provide a message"
android:layout="@layout/preference_auth"/>
此首选项扩展了 EditTextPreference class 所以我希望它在单击首选项后显示 EditText 区域。但是只有空对话框,只有 no 和 yes 按钮。代码如下:
public class AuthPreference extends EditTextPreference {
private ImageView icon;
private EditText et;
public AuthPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setNegativeButtonText("no");
setPositiveButtonText("yes");
}
public AuthPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setNegativeButtonText("no");
setPositiveButtonText("yes");
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
icon = (ImageView) view.findViewById(R.id.iconSelected);
}
@Override
protected void onBindDialogView(View view) {
some code
}
}
怎么了?
事实证明,第二个构造函数是错误的,因为通过设置为 0 的 super 将第三个参数传递给了直接父级。所以在更改之后我有
super(context, attrs);
而不是
super(context, attrs, 0);
您只需要小心并确保将相应的参数传递给特定父级的构造函数。