0

我正在尝试自定义 EditTextPreference 以显示 textview(即显示首选项的值)和右侧的清除/删除按钮。

我创建了CustomEditTextPreference.java

package com.customedittextpreference;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.Image;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * Created by cyong on 23/04/16.
 */
public class CustomEditTextPreference extends EditTextPreference {

private ImageButton clearButton;
private TextView valueTextView;

public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setupChangeListener();
}

public CustomEditTextPreference(Context context, AttributeSet attrs)    {
    super(context, attrs);
    setupChangeListener();
}

public CustomEditTextPreference(Context context) {
    super(context);
    setupChangeListener();
}

@Override
protected void onBindView(View view) {
    super.onBindView(view);

    valueTextView = (TextView) view.findViewById(R.id.value_textview);
    clearButton = (ImageButton) view.findViewById(R.id.clear_button);

    clearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            setText("");

        }
    });

    String valueString = getText();
    Log.v(Settings.APP_NAME, "refreshValue(): valueString=" + valueString);
    valueTextView.setText(valueString);

    toggleClearButton(valueString);

}


private void toggleClearButton(String value)
{
    if (value.length()==0)
    {
        clearButton.setVisibility(View.GONE);
    }
    else
    {
        clearButton.setVisibility(View.VISIBLE);
    }


}

private void setupChangeListener()
{
    setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {

            String newStringValue = (String) newValue;
            valueTextView.setText(newStringValue);

            toggleClearButton(newStringValue);

            return true;
        }
    });
}

}

CustomEditTextPreference 类使用下面的布局(即 prefwidget_edittext.xml)作为小部件布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
android:padding="0dp">
<TextView
    android:id="@+id/value_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_gravity="center_vertical"
    android:singleLine="true" />

<ImageButton
    android:id="@+id/clear_button"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:scaleType="centerInside"
    android:layout_marginRight="5dp"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:layout_gravity="center_vertical"
    android:src="@mipmap/delete_icon"
    android:background="#00000000"
    />
</LinearLayout>

我在 res/xml 下的 preferences_list.xml 中指定了我的自定义 EditTextPreference

 <?xml version="1.0" encoding="utf-8"?>
 <PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
    android:key="status_choice"
    android:entries="@array/array_status_entries"
    android:entryValues="@array/array_status_values"
    android:title="@string/choose_status_title"
    android:summary="%s"
    android:defaultValue="0"
    />
<CheckBoxPreference
    android:defaultValue="false"
    android:key="has_email"
    android:title="@string/has_email_title" >
</CheckBoxPreference>
 <com.customedittextpreference.CustomEditTextPreference
 android:widgetLayout="@layout/prefwidget_edittext"
 android:title="@string/productcode_title"
 android:key="code"/>
</PreferenceScreen>

我可以单击edittextpreference 并输入一个字符串。输入的字符串将被保存,但之后不会显示在我的自定义小部件布局的文本视图中。但是,如果我杀死我的应用程序并重新启动它,textview 将显示保存的字符串。现在,当我单击清除/删除按钮时,我可以看到正在删除的值,但是 UI 没有更新以清除文本视图的字符串并隐藏清除/删除按钮。

为方便起见,我已将示例项目上传到下面的 github 中:

示例 GitHub 项目

4

1 回答 1

0

似乎在更新首选项时需要调用 notifyChanged()。

我注意到调用 setTitle() 和 setSummary() 会更新 UI。事实证明,这两个函数都调用了 notifyChanged()。

使用修复程序更新 github 项目。

GitHub 项目

于 2016-04-26T13:28:00.567 回答