0

我正在嘗試構建一個首選項屏幕,該屏幕由具有一些TextViews和EditText首選項的Layout組成。TextView在首選項屏幕中被覆蓋(而不是重寫)

當我在Activity中使用textView.setText(inputString)時,它不會覆蓋現有的TextView,而是會創建一個TextView的新副本,並將inputString作爲文本並與現有的TextView重疊。有什麼方法可以改變setText覆蓋現有的TextView本身。

這是TextView的

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:background="@color/blue" 
    android:layout_height="match_parent"> 
    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@android:id/list" 
     ></ListView> 
    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:textColor="@color/pseudoBlack" 
     android:layout_height="wrap_content" 
     android:layout_marginStart="59dp" 
     android:layout_marginTop="100dp" 
     android:id="@+id/textView2" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:text="TextView" 
     android:textColor="@color/pseudoBlack" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView5" 
     android:layout_alignBaseline="@+id/textView2" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_toEndOf="@+id/textView2" 
     android:layout_marginStart="100dp" /> 

    <TextView 
     android:text="TextView" 
     android:textColor="@color/pseudoBlack" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="43dp" 
     android:id="@+id/textView11" 
     android:layout_below="@+id/textView2" 
     android:layout_toEndOf="@+id/textView2" 
     android:layout_marginStart="21dp" /> 
</RelativeLayout> 

的XML佈局,這是我使用偏好屏幕。

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

    > 
    <Preference android:layout="@layout/inner" android:key="innerkey" 
     /> 
    <Preference android:key="mskey" 

     android:title="show" 
     /> 
    <EditTextPreference android:key="editpref" 
     android:title="Add" 
     /> 

</PreferenceScreen> 

這是我在其中嘗試執行setText()的Activity類。

package com.example.asus.layoutcheck; 

import android.os.Bundle; 
import android.preference.Preference; 
import android.preference.PreferenceActivity; 
import android.widget.TextView; 

public class SecondaryActivity extends PreferenceActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.inner); 
     TextView txt1 = (TextView)findViewById(R.id.textView2); 
     TextView txt2 = (TextView)findViewById(R.id.textView5); 
     TextView txt3 = (TextView)findViewById(R.id.textView11); 

     txt1.setText("First Text"); 
     txt2.setText("Second Text"); 
     txt3.setText("Third Text"); 

     final String message = "This is the message" ; 
     addPreferencesFromResource(R.xml.pref); 
     final Preference msg = findPreference("mskey"); 
     msg.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { 
      @Override 
      public boolean onPreferenceClick(Preference preference) { 

       msg.setSummary(message); 
       return true; 
      } 
     }); 
     Preference edit = findPreference("editpref"); 

    } 
} 

我已添加結果的屏幕截圖。 enter image description here

我怎樣才能解決這個問題?在此先感謝

回答

0

addPreferencesFromResource(//your preferenceScreen);

+0

它沒有工作更換setContentView(R.layout.inner);。如果我替換setContentView,該應用程序停止工作。 – user6739649