我有一个自定义的TextView类与此代码:的Android - TextView中的值设置为自定义的TextView
public CustomTextView extends TextView {
public CustomTextView(Context c, AttributeSet a) {
super(c, a);
TextView tv = (TextView) findViewById(R.id.myTv);
String str = tv.getText().toString();
setText(str);
}
}
而且我main.xml中包含此:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myTv"
android:text="Unkown" />
<com.test.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="100.0sp" />
我想什么做的是我想要获取TextView的字符串值(带有myTv的ID),并将我的CustomTextView的文本设置为与TextView相同。像TextView = CustomTextView。
myTv的字符串值由sharedpreferences改变,所以每次文本改变时,自定义文本也会改变。
我的问题是如何正确地做到这一点?
在您的自定义视图上设置一个ID并在每次数据集更改时更新它?几乎与更新您的textview的方式相同。如果你想要一个文本侦听器,请查看Android [TextWatcher](https://developer.android.com/reference/android/text/TextWatcher.html)类。 – MeetTitan
是的,这是可能的,但我想要的是,我会离开活动的地方是textview。我希望自定义文本视图能够完成这项工作,并在每次更改文本视图时自行更改。 –
在我评论的最后看看链接,看看我读了你的想法。 'yourTextView.addTextChangedListener(new TextWatcher(){...});' – MeetTitan