Java Android:编辑xml中定义的文本视图

Java Android:编辑xml中定义的文本视图,java,android,xml,android-layout,Java,Android,Xml,Android Layout,我已经坐了至少4个小时试图解决这个问题 要了解这一点,您需要了解以下三个文件: java扩展了Activity,该类的用途仅限于 保存游戏状态并显示选项菜单 java,它扩展了SurfaceView并包含“游戏” eggCatcher_layout.xml,如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res

我已经坐了至少4个小时试图解决这个问题

要了解这一点,您需要了解以下三个文件:

java扩展了Activity,该类的用途仅限于 保存游戏状态并显示选项菜单

java,它扩展了SurfaceView并包含“游戏”

eggCatcher_layout.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layouten">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <easter.fun.EggCatcherView
            android:id="@+id/eggcatcher"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <RelativeLayout android:id="@+id/relativeLayout1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true">

            <TextView android:text="Score: " 
                android:id="@+id/totalscore" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true">
             </TextView>

             <TextView android:text="Bonus: "
                android:id="@+id/bonus" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true">
        </TextView>

        </RelativeLayout>
    </FrameLayout>
</LinearLayout>

如xml文件所示,EggCatcherView放在xml文件中。 当我启动应用程序时,onCreate调用setContentView(layout.egggcatcher\u layout)

我现在的问题是: 如何从egggatcherview.java访问和编辑XML文件中定义的文本视图

如果它是在egggcatcher.java中,那么就很容易了,只需使用findviewbyd(id.bonus),但是从 在surfaceView内部,似乎有点困难

我希望我已经把一切都说清楚了,如果你不明白,就问吧


//Mike

我认为你应该获得父视图,然后从那里开始你可以使用
findViewById()
(你确定你不能只使用这个方法吗,因为
SurfaceView
view
的一个子类,并从中继承
findViewById()

要使用父对象,请执行以下操作:

ViewParent vp = eggCatcherView.getParent();
FrameLayout fl = (FrameLayout) vp;
TextView tx  = (TextView) fl.findViewById(R.id.bonus);

当然,您需要检查ViewParent是否确实是FrameLayout的实例。

我发现这是最好的方法:

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:id="@+id/test"/>
</LinearLayout>

TextView test = (TextView) findViewById(R.id.test);
test.setText("test");

TextView测试=(TextView)findViewById(R.id.test);
test.setText(“测试”);

如果我理解正确,您想访问周围活动中的视图吗?那看起来像是糟糕的建筑。我认为最好是将回调传递给EggCatcherView,该回调可以触发活动中的方法,这些方法反过来操作TextView,或者向上触发某种事件。

感谢您的回答,正如您所说,SurfaceView提供了findViewById()。不幸的是,它找不到TextView并返回null。如果我没弄错的话,从一个视图中只找到视图中的孩子。我不知道这对我意味着什么。我的第二个理论是onCreate中的setContentView还没有出现,只是还不可能找到TextView。如果是这种情况,是否有某种方法可以同步Eggcatcher中的构造函数,直到Eggcatcher中的setContentView完成?好的,对孩子们来说是有意义的。