【问题标题】:Android: edit textview defined in xmlAndroid:编辑xml中定义的textview
【发布时间】:2011-02-20 18:53:41
【问题描述】:

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

要理解这一点,您需要了解 3 个文件:

eggCatcher.java 扩展了Activity,这个类用的不多 保存游戏状态并显示选项菜单。

eggCatcherView.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.eggcatcher_layout);

我现在的问题是: 我如何从 Egg​​CatcherView.java 访问和编辑 xmlfile 中定义的 TextViews?

如果它在 EggCatcher.java 中会很容易,只需使用 findViewById(id.bonus),但从 在surfaceView里面似乎有点困难。

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

//米克

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    我认为你应该得到父视图,然后从那里你可以使用findViewById()(你确定你不能只使用那个方法,因为SurfaceViewView的子类并继承@987654324 @来自它?)。

    要使用父级,您可以执行以下操作:

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

    当然你需要检查 ViewParent 是否确实是 FrameLayout 的一个实例。

    【讨论】:

    • 感谢您的回答,正如您所说,SurfaceView 提供了一个 findViewById()。不幸的是,它没有找到 TextView 并返回 null。如果我从视图中找到正确的 findViewById 只会找到该视图的子视图。我不知道这对我意味着什么。我的第二个理论是 onCreate 中的 setContentView 还没有完成,而且它还不可能找到 textViews。如果是这样的话,有没有办法同步 EggCatcherView 中的构造函数等到 Eggcatcher 中的 setContentView 完成?
    • 好的,对孩子来说很有意义。
    【解决方案2】:

    我发现这是最好的方法:

    <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");
    

    【讨论】:

      【解决方案3】:

      如果我理解正确,您想访问周围活动中的视图吗?这似乎是糟糕的架构。我认为最好将回调传递给 EggCatcherView,该回调可以触发 Activity 中的方法,这些方法反过来对 TextViews 进行操作或向上触发某种事件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-28
        相关资源
        最近更新 更多