【问题标题】:custom EditTextPreference: can't get TextView by id自定义 EditTextPreference:无法通过 id 获取 TextView
【发布时间】:2013-01-22 09:19:45
【问题描述】:

我已经创建了一个自定义的 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 值,没关系。但是当我设置文本时,我得到一个空异常,告诉我 TextView 是空的。

这是我的偏好代码:

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 布局的一部分)?

【问题讨论】:

    标签: android preference edittextpreference


    【解决方案1】:

    简单的膨胀你的 xml

    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.custome_layout, null);
    
    mTextViewAlertPlanned = (TextView) view.findViewById(R.id.alert_planed);
    

    【讨论】:

    • android.content.res.Resources$NotFoundException: 字符串资源 ID #0x6
    • 检查您项目的 values 文件夹中的 strings.xml 并确保所有字符串资源都在那里。
    • 我确定我有我所有的字符串,我认为这里的问题是我们创建了一个屏幕上不存在的新布局
    • 01-22 11:00:37.371: E/AndroidRuntime(10124): android.content.res.Resources$NotFoundException: 字符串资源 ID #0x6 01-22 11:00:37.371: E/ AndroidRuntime(10124): 在 android.content.res.Resources.getText(Resources.java:229) 01-22 11:00:37.371: E/AndroidRuntime(10124): 在 android.widget.TextView.setText(TextView.java :3620) 01-22 11:00:37.371: E/AndroidRuntime(10124): 在 fr.haploid.preferenceprotoptvo.Preference$1.onPreferenceChange(Preference.java:59)
    【解决方案2】:

    我遇到了同样的问题,对我有用的是创建一个从 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())

    希望有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多