简体   繁体   中英

Android: edit textview defined in xml

I have been sitting for at least 4 hours trying to solve this problem.

To understand this there are 3 files you need to know about:

eggCatcher.java which extends Activity, this class is not used for much more than saving gamestate and showing the optionsmenu.

eggCatcherView.java which extends SurfaceView and contains "the game".

eggCatcher_layout.xml which is shown below:

<?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>

As shown in the xml file, EggCatcherView is put in the xml file. When the applications i started the onCreate calls setContentView(layout.eggcatcher_layout);

My question now is: how can i, from EggCatcherView.java access and edit the TextViews defined in the xmlfile?

if it was in EggCatcher.java it would be easy, just use findViewById(id.bonus), but from inside the surfaceView appears to be a little more difficult.

I hope i have made everything clear, if you dont understand just ask!

//micke

If I understand correctly, you want to access the views in the surrounding activity? That seems like poor architecture. I think it would be better to either pass a callback to the EggCatcherView that can trigger methods in the Activity which in turn operate on the TextViews or fire some kind of events upwards.

I think you should get the parent view and then from there on you can use findViewById() (are you sure you can't just use that method anyway since SurfaceView is a subclass of View and inherits findViewById() from it?).

For using the parent you do something like:

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

Of course you need to check if the ViewParent is indeed an instance of FrameLayout.

I found this the best way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM