2012-03-21 2 views
2

how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-summary에 의해 영감을 얻은 저는 자신의 EditTextPreference를 만들었습니다.이 EditTextPreference는 PreferenceScreen에서 현재 값을 보여줍니다.현재 값을 가진 환경 설정 요약 사용자 정의 EditTextPreference : 현재 값을 얻는 방법?

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen android:key="preferences" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <PreferenceCategory 
     android:title="First Category" 
     android:key="first_category"> 

     <de.k3b.widgets.EditTextPreferenceWithSummary 
      android:key="test" 
      android:title="Test Message" 
      android:dialogTitle="Test Message" 
      android:dialogMessage="Provide a message" 
      android:defaultValue="Default welcome message" /> 

    </PreferenceCategory> 
</PreferenceScreen> 

구현은 PreferenceScreen 먼저 표시 전류 값이없는 도시이

package de.k3b.widgets; 

import android.content.Context; 
import android.content.SharedPreferences; 
import android.preference.*; 
import android.util.AttributeSet; 
import android.util.Log; 

public class EditTextPreferenceWithSummary extends EditTextPreference { 
    private final static String TAG = EditTextPreferenceWithSummary.class.getName(); 

    public EditTextPreferenceWithSummary(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public EditTextPreferenceWithSummary(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     Log.e(TAG, "init"); 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());  
     String currentText = test prefs.getString("minTrashholdInSecs", this.getText()); 

// where do i get the current value of the underlaying EditTextPreference ?? 
//  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 
     this.setSummary(this.getText()); 
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

     setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 

      @Override 
      public boolean onPreferenceChange(Preference preference, Object newValue) { 
       Log.w(TAG, "display score changed to "+newValue); 
       preference.setSummary(newValue.toString()); // getSummary()); 
       return true; 
      } 
     }); 
    } 
} 

같다.

내 문제 : 어디에서 EditTextPreference가 나타내는 현재 값을 얻을 수 있습니까? getText()는 예상 한 값을 가져 오지 않습니다. preferencevalue를 변경하면이 값이 요약 필드에 예상대로 표시됩니다.

임은 EditTextPreferenceWithSummary 클래스에 다음 코드를 추가 안드로이드 2.2

답변

3

시도를 사용하고 있습니다 :

나를 위해로서
@Override 
protected View onCreateView(ViewGroup parent) { 
    this.setSummary(this.getText()); 
    return super.onCreateView(parent); 
} 

, 그것은 작동합니다. 문제는 UI 스레드 (실제로 생성자에서)의 UI 상태를 변경하려고했다는 것입니다.

2

다음은 환경 설정 수신기를 설치하지 않고도 값이 업데이트되는 경우를 포함하는보다 완벽한 예입니다.

import android.content.Context; 
import android.preference.EditTextPreference; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 

public class EditTextPreferenceWithValueSummary extends EditTextPreference{ 

    public EditTextPreferenceWithValueSummary(Context context) { 
    super(context); 
    } 

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

    @Override 
    protected View onCreateView(ViewGroup parent) { 
     this.setSummary(this.getText()); 
     return super.onCreateView(parent); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
     super.onDialogClosed(positiveResult); 

     if (positiveResult) { 
     this.setSummary(getText()); 
     } 
    } 
} 

그리고 당신의 xml/settings.xml

:

<your.package.views.EditTextPreferenceWithValueSummary 
     android:key="some_preference_key" 
     android:title="Some Title" /> 
+0

하나는 사실이 soulution은 나보다 더 우아하다. @Oleg Flores는 2012 년에 제 문제를 해결했기 때문에 대답을 받았습니다. – k3b

관련 문제