我有以下情况: 在我做的主文件中 的setContentView(R.layout.main); 看起来如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/result"
android:text="Gesamtstrecke: 0.0"
/>
<test.gustav.Spielfeld
android:id="@+id/spielfeld"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</test.gustav.Spielfeld>
</LinearLayout>
视图“test.gustav.Spielfeld”指的是一个扩展“视图”的“Spielfeld”类。
我现在需要一个方法,如果在Spielfeld-View中的onTouchEvent(..) - 方法中发生某些事情,则使用id“result”更新TextView的内容。
怎么做?我尝试在Main-Class中创建一个公共TextView,但Spielfeld类不接受Main.this.myTextView。
答案 0 :(得分:4)
我会推荐一种回调方法(就像android对所有事件一样):
创建一个代表回调的界面:
public interface OnSpielfeldUpdate {
public void onSpielfeldUpdate(Object myEventData, ...);
}
为SpielFeld
中的回调提供一个setter(将用于发送更新):
public void setOnSpielfeldUpdate(OnSpielfeldUpdate callback) {
this.callback = callback;
}
在您的活动(或其他地方)设置回调:
mySpielfeld.setOnSpielfeldUpdate(new OnSpielfeldUpdate() {
public void onSpielfeldUpdate(Object myEventData, ...) {
// perform update on result view
}
}