我有一个带有以下代码的自定义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的字符串值(ID为myTv),并将CustomTextView的文本设置为与TextView相同。像TextView = CustomTextView。
myTv的字符串值由sharedpreferences更改,因此每次文本更改时,自定义文本也会更改。
我的问题是如何正确地做到这一点?
答案 0 :(得分:0)
这非常简单,您只需初始化两个textviews并从一个获取字符串并将其设置为另一个。使用TextView扩展您的类可以为您提供TextView可以使用的所有方法。
CustomTextView customtv = (CustomTextView) findViewById(R.id.customTv);
TextView tv = (TextView) findViewById(R.id.myTv);
customtv.setText(tv.getText());
只需在CustomTextView上设置id,无论你想要什么,我都会使用customTv id。
修改强>
据我所知,你想要textChangeListener,这里是example。