自定义EditTextPreference:无法通过id获取TextView

时间:2013-01-22 09:19:46

标签: android preference edittextpreference

我创建了一个自定义的EditTextPreference:

<EditTextPreference
                android:key="alert_planed_edittext_preference"
                android:title="@string/alert_planed_edittext_preference"
                android:summary="@string/alert_planed_summary_edittext_preference"
                android:dialogTitle="@string/alert_planed_dialog_title_edittext_preference"
                android:numeric="integer"
                android:layout="@layout/edit_text_preference_layout" />

自定义布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/alert_planed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dip"
        android:text="5" >
    </TextView>


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="6dip" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="2"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </RelativeLayout>

</RelativeLayout>

我刚添加了id为alert_planed的文本视图。

在我的偏好活动中,我试图获取新的EditTextPreference值并将其放在我的文本视图alert_planed中。 我得到了EditTextPreference值,没关系。但是当我设置文本时,我得到一个null异常,告诉我TextView为null。

这是我的偏好代码:

addPreferencesFromResource(R.xml.preferences);

        mTextViewAlertPlanned = (TextView) findViewById(R.id.alert_planed);

        findPreference("alert_planed_edittext_preference").setOnPreferenceChangeListener(new OnPreferenceChangeListener()
        {

            @Override
            public boolean onPreferenceChange(
                    android.preference.Preference arg0, Object value) {

                if(value != null)
                {

                    Log.i("Preference","String.valueOf(value) ="+String.valueOf(value));

                    Log.i("Preference","mTextViewAlertPlanned ="+mTextViewAlertPlanned);

                    int myVal = Integer.parseInt(String.valueOf(value));

                    mTextViewAlertPlanned.setText(myVal);
                }
                return true;
            }
        });

如何正确获取TextView(TextView是自定义EditTextPreference布局的一部分)?

2 个答案:

答案 0 :(得分:0)

简单膨胀你xml

LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.custome_layout, null);

mTextViewAlertPlanned = (TextView) view.findViewById(R.id.alert_planed);

答案 1 :(得分:0)

您遇到了同样的问题,而对我有用的是创建一个从EditTextPreference开始的类:

class CustomEditTextPreference constructor(
    context: Context,
    attrs: AttributeSet) : EditTextPreference(context, attrs) {

    lateinit var tvTitle: TextView
    lateinit var summary: TextView

    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)
        with(holder.itemView) {
            // Here would go your ids
            tvTitle = findViewById(R.id.tv_title)
            summary = findViewById(R.id.tv_summary)
        }
    }

    // Whenever you want to change the 'summary' you call this function and pass the value as parameter
    fun changeSummary(value: String){
        summary.text = value
    }
}

preferenceFragment中,不要这样做:

preference.summary = newValue.toString()

您只需调用该函数: changeSummary(newValue.toString())

希望有效!

相关问题