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");
}
}
我怎样才能解决这个问题?在此先感谢
它没有工作更换
setContentView(R.layout.inner);
。如果我替换setContentView,该应用程序停止工作。 – user6739649