2016-04-24 36 views
0

我想自定义EditTextPreference来显示一个Textview(即显示首选项的值)和一个清除/删除按钮在其右侧。Android自定义EditTextPreference UI没有得到更新

我创建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> 

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

<?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未更新以清除字符串textview并隐藏清除/删除按钮。

为了方便起见,我已经上传我的样本项目到下面GitHub上:

Sample GitHub Project

回答

0

好像notifyChanged()需要更新的偏好时调用。

我注意到了调用setTitle()和setSummary()会更新UI。事实证明,notifyChanged()在那些函数中都被调用。

用修正更新github项目。

GitHub Project