2013-08-29 3 views
1

J'utilise PreferenceFragment avec des préférences par défaut catégories et une mise en page personnalisée:TextView setText ne fonctionne pas sur la mise en page custome dans PreferenceFragment

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

    <PreferenceCategory 
     android:key="first_category" 
     android:title="config" > 
     <EditTextPreference 
      android:defaultValue="example" 
      android:dialogMessage="text" 
      android:dialogTitle="Title" 
      android:key="mykey" 
      android:summary="summary" 
      android:title="title" /> 

     <Preference 
      android:key="test_connection" 
      android:title="Test connection" 
      android:layout="@layout/test_conn" /> 
</PreferenceCategory> 

</PreferenceScreen> 

Ma mise en page de test_conn:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="6dip" 
    android:layout_marginLeft="15dip" 
    android:layout_marginRight="6dip" 
    android:layout_marginTop="6dip" 
    android:layout_weight="1" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="20dp" 
     android:ellipsize="marquee" 
     android:fadingEdge="horizontal" 
     android:text="Test connection" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/summary" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="20dp" 
     android:ellipsize="marquee" 
     android:fadingEdge="horizontal" 
     android:text="Ko" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#ff0000" 
     android:textStyle="bold" /> 

</RelativeLayout> 

Je suis lire le texte R.id.summary avec ce code:

 addPreferencesFromResource(R.xml.preferences); 
     findPreference(TEST_CONN).setOnPreferenceClickListener(this);  
     Preference custom = findPreference(TEST_CONN); 
     customv = custom.getView(null, null); 
     res = (TextView) customv.findViewById(R.id.summary); 

Je peux lire cor directement mon TextView, mais si j'essaie de changer le texte, par exemple dans une asynctask, je ne peux pas voir le texte changé.

Ceci est une partie de AsyncTask:

 @Override 
    protected void onPostExecute(Boolean result) { 
     if (result == true){ 
      res.setText("Ok"); 
      res.setTextColor(Color.GREEN); 
     } 
     else { 
      res.setText("Ko"); 
      res.setTextColor(Color.RED); 
     } 
     pdia.dismiss(); 
    } 

Lorsque l'utilisateur clique sur la préférence « test_connection », commence un AsyncTask et dans le Funzion onPostExecute je modifie le texte, mais ne fonctionne pas. Est-ce que je manque quelque chose? Devrais-je lire le textview avec une autre méthode?

Répondre

1

Voici une solution : Vous devez implémenter une classe de préférence personnalisée qui étend la classe Android Preference. Assurez-vous que votre classe personnalisée se trouve dans un fichier séparé - ne faisant pas partie d'une autre classe - sinon vous obtiendrez une 'erreur XML de gonflage' comme this. Voici un exemple:

Mon CustomPreference.java fichier:

public class CustomPreference extends Preference { 
    private TextView txt; 

    public CustomPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.setWidgetLayoutResource(R.layout.test_conn); 
    } 

    @Override 
    protected void onBindView(View view) { 
     super.onBindView(view); 
     txt = (TextView) view.findViewById(R.id.summary); 
    } 

    public void setText(String text, int color) { 
     txt.setText(text); 
     txt.setTextColor(color); 
    } 
} 

Votre fichier settings.xml doit insérer/utiliser la classe personnalisée préférence comme ceci:

<com.myapp.example.widget.CustomPreference 
     android:name="com.myapp.example.widget.CustomPreference" 
     android:key="test_connection" 
     android:title="Test connection" 
     android:layout="@layout/test_conn" /> 

Lorsque vous vouloir modifier le TextView ou le bouton sur votre mise en page personnalisée, vous devez appeler la méthode publique setText(), fournissant des paramètres comme le texte et la couleur:

 @Override 
    protected void onPostExecute(Boolean result) { 
     if (result == true){ 
      CustomPreference custom = (CustomPreference) findPreference(TEST_CONN); 
      custom.setText("Ok", Color.GREEN); 
     } 
     else { 
      CustomPreference custom = (CustomPreference) findPreference(TEST_CONN); 
      custom.setText("Ko", Color.RED); 
     } 
     pdia.dismiss(); 
    } 

Voir aussi: Another answer here

+0

Sky Il n'a pas fonctionné pour moi .. – Asthme

Questions connexes