Java/Android 从片段更改布局内的文本

Java/Android change text inside layout from a fragment

我的第一个应用程序有问题,这也是我的第一个问题,我有 2 activity 和 2 layout

这是MainActivit.java

public class MainActivity extends TabActivity {

    TabHost tabHost;

    public static int meth;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LoadAllVar();

        UpdateHud(meth);

        TabSwitcher();

    }

    public static int  LoadAllVar(){

        //Load all var from files (not added for now)
        meth = 200;

        return meth;
    }

    private void  UpdateHud(int meth){

        String methstring = Integer.toString(meth);
        TextView textSell = (TextView)findViewById(R.id.textMethCount);
        textSell.setText(methstring);
    }

    public void TabSwitcher() {

        tabHost = getTabHost();

        TabHost.TabSpec tab1spec = tabHost.newTabSpec("Tab1");
        tab1spec.setIndicator("Cook");
        Intent firstintent = new Intent(this, CookTab.class);
        tab1spec.setContent(firstintent);
        tabHost.addTab(tab1spec);
    }
}

这是第二个Activity

public class CookTab extends Activity {

    public static int meth;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cooktab);

        LoadVarFromCook();

        CheckCookClick();


    }

    public void LoadVarFromCook (){
        meth = LoadAllVar();
        String methstring = Integer.toString(meth);
        Toast.makeText(getApplicationContext(),methstring,Toast.LENGTH_SHORT).show();
    }

    public void CheckCookClick (){

        Button buttoncook = (Button) findViewById(R.id.buttonCook);
        buttoncook.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){

                meth = meth + 1;
                Toast.makeText(getApplicationContext() , "+1" , Toast.LENGTH_SHORT).show();

                //HERE I NEED TO UPDATE THE VALUE OF METH INSIDE THE ACTIVITY_MAIN.XML
            }

        });
    }
}

这是 MainActivity 布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TabHost
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:id="@android:id/tabhost">

        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"></TabWidget>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent"></FrameLayout>

    </TabHost>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:id="@+id/layoutHUD">

        <TextView
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:id="@+id/textMethCount"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="28dp"
            android:layout_marginStart="28dp" />

    </RelativeLayout>
</RelativeLayout>

这是第二个tab layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:id="@+id/textMeth"
        android:layout_marginTop="250dp"
        android:layout_marginLeft="145dp"
        android:text="Meth"
        android:gravity="center"
        android:textSize="29dp"
        android:textStyle="bold"/>

    <Button
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:id="@+id/buttonCook"
        android:layout_below="@+id/textMeth"
        android:layout_alignLeft="@+id/textMeth"
        android:layout_alignStart="@+id/textMeth"
        android:text="Cook"/>

</RelativeLayout>

当我按下 cooktab.xml 内的按钮时,我需要更改位于 activity_main.xml 内的 TextView 内的文本,我该怎么做? findViewById() 不起作用

P.S。现在我只添加一个选项卡,我会添加更多选项卡但现在我考虑第一个

尝试使用 EventBus it allows you to call functions in other parts of your app. Alternatively use a standard java interface link

我按照本教程解决了问题http://mrbool.com/how-to-create-an-activity-android-with-a-tabhost-view/27990