2016-04-24 10 views
0

EditTextPreferenceをカスタマイズして、テキストビュー(つまり、設定の値を表示する)とその右側にクリア/削除ボタンを表示しようとしています。AndroidカスタムEditTextPreference UIが更新されない

は私が

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クラスは

<?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を指定するウィジェットレイアウトとして(すなわちprefwidget_edittext.xml)以下のレイアウトを使用CustomEditTextPreference.javaを作成しました下の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ビューは文字列をクリアしてクリア/削除ボタンを隠すように更新されていません。便宜上

、私は下にgithubのに私のサンプルプロジェクトをアップロードしています

Sample GitHub Project

答えて

0

notifyChangedは()選好を更新したときに呼び出される必要があるように思えます。

setTitle()とsetSummary()がUIを更新することに気付きました。それは、これらの両方の関数でnotifyChanged()が呼び出されたことが判明しました。

修正プログラムでgithubプロジェクトを更新します。

GitHub Project

関連する問題