片段中级(三):创建活动的的onclick各个​​按钮片段之间交替片段、创建活动、按钮、onclick

2023-09-07 22:25:34 作者:靠自己才能当上女王

目标:

创建有两个按钮,按钮的活动1和按钮2.点击,该片段将在两个片段之间切换。

背景:

Fragmentone将采取从片段上的TextView的主要活动和的setText的EditText上。 Fragmenttwo将显示文本的tablelayout。 (我的最终目标是利用输入从主activty并显示表中的TextView的之一,但它不能正常工作,请告知上)##问题1

问题:

这应该显示fragmentone不工作Button1的。键控输入插入mainactivity的EditText上后,当我点击该按钮,fragmenttwo坚持。 FragmentOne没有出来。放置一个土司fragmentone和烤面包片显示我的输入。

特别鸣谢: 想在这个社会上特别感谢乐于助人的人指导我到这地步。而会想致以感谢Raghunandan我谁引导我通过片段提前,二。谢谢你了,通过我的学习之旅协助我。希望我的问题,raghunandan意见将有助于降低学习曲线为那些embraking旅程的起点。

片段中级(我)

Fragment中级(一):从EditText上获得的输入,在片段的TextView

设置文本

片段中间体(II)的

Fragment中间体(II):Dynmically添加在一个片段行,机器人的

MainActivity.java

 进口android.os.Bundle;
进口android.app.Activity;
进口android.app.Fragment;
进口android.app.FragmentManager;
进口android.app.FragmentTransaction;
进口android.view.Menu;
进口android.view.View;

公共类MainActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    }

    公共无效selectFrag(查看视图){
        片段FR;
        如果(查看== findViewById(R.id.button2)){
            FR =新FragmentTwo();

        }
        其他 {
            FR =新FragmentOne();
        }
        FragmentManager FM = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place,法国);
        fragmentTransaction.commit();
   }
}
 

activity_main.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <的FrameLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>

            < TableLayout
                机器人:ID =@ + ID / TableLayout01
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:stretchColumns =1>

                <的TableRow
                    机器人:ID =@ + ID / tableRow1
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT>

                    <的TextView
                        机器人:ID =@ + ID / dis_input
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:文本=输入/>

                    <的EditText
                        机器人:ID =@ + ID /输入
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:EMS =10/>
                < /的TableRow>



                <的TableRow
                    机器人:ID =@ + ID / tableRow9
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT>

                <按钮
                    机器人:ID =@ + ID /按钮2
                    机器人:layout_width =match_parent
                    机器人:layout_height =WRAP_CONTENT
                    机器人:的onClick =selectFrag
                    机器人:文本=Button2的/>

                <按钮
                    机器人:ID =@ + ID /按钮1
                    机器人:layout_width =match_parent
                    机器人:layout_height =WRAP_CONTENT
                    机器人:的onClick =selectFrag
                    机器人:文本=计算/>
                < /的TableRow>

            < / TableLayout>
    < /的FrameLayout>
    <片段
        机器人:名称=com.example.sample.FragmentTwo
        机器人:ID =@ + ID / fragment_place
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>

< / LinearLayout中>
 

FragmentOne.java

 公共类FragmentOne扩展片段{

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
    {
        查看查看= inflater.inflate(R.layout.fragment_one,集装箱,假);
        TextView的魔兽=(TextView中)view.findViewById(R.id.moargold);
        EditText上moargold =(EditText上)getActivity()findViewById(R.id.input)。
        双vespenegas = Double.parseDouble(moargold.getText()的toString());

        warcraft.setText(新双(vespenegas)的ToString());
        吐司面包= Toast.makeText(getActivity(),新的双(vespenegas)的ToString(),Toast.LENGTH_SHORT);
        toast.show();
        返回查看;
    }
}
 

fragment_one.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

             < TableLayout
                机器人:ID =@ + ID / TableLayout01
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:stretchColumns =1>

                <的TableRow
                    机器人:ID =@ + ID / tableRow4
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT>

                    <的TextView
                        机器人:ID =@ + ID / dis_moargold
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:文本=金伯爵/>

                    <的TextView
                        机器人:ID =@ + ID / moargold
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:文本=TextView的/>
                < /的TableRow>
 < / TableLayout>
< / LinearLayout中>
 

FragmentTwo.java

 公共类FragmentTwo扩展片段{

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
    {
        查看查看= inflater.inflate(R.layout.fragment_two,集装箱,假);
        EditText上输入=(EditText上)getActivity()findViewById(R.id.input)。



        TableLayout TL =(TableLayout)view.findViewById(R.id.mainLayout);


        的TableRow TR =新的TableRow(getActivity());
        tr.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        TextView的textview1 =新的TextView(getActivity());
        textview1.setText(快乐);
        tr.addView(textview1);

        TextView的textview2 =新的TextView(getActivity());
        textview2.setText(不高兴);
// ###############从EditView中到表中插入文本
//双buygas = Double.parseDouble(input.getText()的toString());
// textview2.setText(新双(buygas)的ToString());
        tr.addView(textview2);

        tl.addView(TR,新TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        返回查看;
    }
}
 

fragment_two.xml

 < XML版本=1.0编码=UTF-8&GT?;

<的LinearLayout
   的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
   机器人:layout_width =match_parent
   机器人:layout_height =match_parent
   机器人:方向=垂直
   机器人:后台=#FFFF00>


        < TableLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:ID =@ + ID / mainLayout>

        <的TableRow
                机器人:ID =@ + ID / infoRow
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT>

                <的TextView
                        机器人:文本=第一
                        机器人:ID =@ + ID /列1
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT>
                < / TextView的>

                <的TextView
                        机器人:文本=第二
                        机器人:ID =@ + ID /列2
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT>
                < / TextView的>


        < /的TableRow>
< / TableLayout>
< / LinearLayout中>
 

解决方案

只是有的FrameLayout 的EditText activity_main.xml 的FrameLayout 是一个容器(ViewGroup中),向其中添加或更换片段。你的 TableLayout 可以在片段布局。

您添加/替换片段编程等什么是需要具有相同的XML格式。

在activity_main.xml有这种

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的EditText
        机器人:ID =@ + ID /输入
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:EMS =10>

        <不是requestFocus />
    < /的EditText>

    <的FrameLayout
        机器人:ID =@ + ID /容器// id是集装箱
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_above =@ + ID /按钮1
        机器人:layout_below =@ + ID /输入
      >
    < /的FrameLayout>

    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_alignRight =@ + ID /输入
        机器人:的onClick =selectFrag
        机器人:文本=Button1的/>

    <按钮
        机器人:ID =@ + ID /按钮2
        机器人:的onClick =selectFrag
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID /输入
        机器人:layout_alignParentBottom =真
        机器人:文本=Button2的/>

< / RelativeLayout的>
 

然后在MainActivity

 公共类MainActivity延伸活动{
    片段FR;
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

    }

    公共无效selectFrag(查看视图){

        如果(查看== findViewById(R.id.button2)){
            FR =新FragmentTwo();

        }
        其他 {
            FR =新FragmentOne();
        }
        FragmentManager FM = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.container,法国);
        fragmentTransaction.commit();
   }
}
 

FragmentOne

 公共类FragmentOne扩展片段{

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
    {
        查看查看= inflater.inflate(R.layout.frag1,集装箱,假);
        TextView的魔兽=(TextView中)view.findViewById(R.id.moargold);
        EditText上moargold =(EditText上)getActivity()findViewById(R.id.input)。
        双vespenegas = Double.parseDouble(moargold.getText()的toString());

        warcraft.setText(将String.valueOf(vespenegas));
        吐司面包= Toast.makeText(getActivity(),将String.valueOf(vespenegas),Toast.LENGTH_SHORT);
        toast.show();
        返回查看;
    }
}
 

frag1.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

             < TableLayout
                机器人:ID =@ + ID / TableLayout01
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:stretchColumns =1>

                <的TableRow
                    机器人:ID =@ + ID / tableRow4
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT>

                    <的TextView
                        机器人:ID =@ + ID / dis_moargold
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:文本=金伯爵/>

                    <的TextView
                        机器人:ID =@ + ID / moargold
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:文本=TextView的/>
                < /的TableRow>
 < / TableLayout>
< / LinearLayout中>
 

FragmentTwo

 公共类FragmentTwo扩展片段{

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
    {
        查看查看= inflater.inflate(R.layout.frag2,集装箱,假);
        EditText上输入=(EditText上)getActivity()findViewById(R.id.input)。



        TableLayout TL =(TableLayout)view.findViewById(R.id.TableLayout01);


        的TableRow TR =新的TableRow(getActivity());
        tr.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        TextView的textview1 =新的TextView(getActivity());
        textview1.setText(快乐);
        tr.addView(textview1);

        TextView的textview2 =新的TextView(getActivity());
        textview2.setText(不高兴);
// ###############从EditView中到表中插入文本
//双buygas = Double.parseDouble(input.getText()的toString());
// textview2.setText(新双(buygas)的ToString());
        tr.addView(textview2);

        tl.addView(TR,新TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        返回查看;
    }
}
 

frag2.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

             < TableLayout
                机器人:ID =@ + ID / TableLayout01
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:stretchColumns =1>

                <的TableRow
                    机器人:ID =@ + ID / tableRow4
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT>

                    <的TextView
                        机器人:ID =@ + ID / dis_moargold
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:文本=金伯爵/>

                    <的TextView
                        机器人:ID =@ + ID / moargold
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =WRAP_CONTENT
                        机器人:文本=TextView的/>
                < /的TableRow>
 < / TableLayout>
< / LinearLayout中>
 

Aim:

Create a activity that have two buttons, button 1 and button 2. When click, the fragment will alternate between the two fragments.

Background:

Fragmentone will take the edittext from the main activity and settext on the textview on the fragment. Fragmenttwo will take display text to the tablelayout. (My ultimate goal is to use the input from the main activty and display it to one of the textview of the table, but it is not working, kindly advise on that)##issue 1

Problems:

The button1 that is supposed to display fragmentone is not working. After keying in the input into the edittext on the mainactivity, when i click the button, the fragmenttwo persist. FragmentOne did not come out. Place an toast in fragmentone and the toast display my input.

Special Thanks to: Would like to extend a special thanks to helpful people in this community for guiding me this far. And will like to extend my thanks to Raghunandan who guide me through fragment advance I, II. Thanks you very much for assisting me through my learning journey. Hope my questions and raghunandan advice would help reduce the learning curve for those embraking on this journey.

Fragment Intermediate (I)

Fragment Intermediate(I):Getting input from edittext, set text in textview of a fragment

Fragment Intermediate (II)

Fragment Intermediate (II):Dynmically adding row in a fragment, Android

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

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

    public void selectFrag(View view) {
        Fragment fr;    
        if(view == findViewById(R.id.button2)) {
            fr = new FragmentTwo();

        }
        else {
            fr = new FragmentOne();
        }
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place, fr);
        fragmentTransaction.commit();
   }
}

activity_main.xml

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

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

            <TableLayout
                android:id="@+id/TableLayout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="1" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/dis_input"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Input" />

                    <EditText
                        android:id="@+id/input"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:ems="10" />
                </TableRow>



                <TableRow
                    android:id="@+id/tableRow9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/button2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:onClick="selectFrag"
                    android:text="Button2" />

                <Button
                    android:id="@+id/button1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:onClick="selectFrag"
                    android:text="Calculate" />
                </TableRow>

            </TableLayout>
    </FrameLayout>
    <fragment
        android:name="com.example.sample.FragmentTwo"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

FragmentOne.java

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        TextView warcraft=  (TextView) view.findViewById(R.id.moargold);
        EditText moargold = (EditText) getActivity().findViewById(R.id.input);
        Double vespenegas = Double.parseDouble(moargold.getText().toString());

        warcraft.setText(new Double(vespenegas).toString());
        Toast toast = Toast.makeText(getActivity(),new Double(vespenegas).toString() , Toast.LENGTH_SHORT);
        toast.show();
        return view;
    }
}

fragment_one.xml

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

             <TableLayout
                android:id="@+id/TableLayout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="1" >

                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/dis_moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Gold Count:" />

                    <TextView
                        android:id="@+id/moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />
                </TableRow>
 </TableLayout>
</LinearLayout>

FragmentTwo.java

public class FragmentTwo extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        EditText input = (EditText) getActivity().findViewById(R.id.input);



        TableLayout tl=(TableLayout) view.findViewById(R.id.mainLayout);


        TableRow tr = new TableRow(getActivity());
        tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        TextView textview1 = new TextView(getActivity());
        textview1.setText("happy");
        tr.addView(textview1);

        TextView textview2 = new TextView(getActivity());
        textview2.setText("unhappy");
//###############To insert text from editview to table
//      Double buygas = Double.parseDouble(input.getText().toString());
//        textview2.setText(new Double(buygas).toString());
        tr.addView(textview2);

        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        return view;
    }
}

fragment_two.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:orientation="vertical"
   android:background="#ffff00">


        <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainLayout">

        <TableRow
                android:id="@+id/infoRow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                        android:text="First"
                        android:id="@+id/column1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                </TextView>

                <TextView
                        android:text="Second"
                        android:id="@+id/column2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                </TextView>


        </TableRow>
</TableLayout>  
</LinearLayout>

解决方案

Just have the FrameLayout and EditText in activity_main.xml. FrameLayout is a container (ViewGroup) to which you add or replace fragments. Your TableLayout can be in fragment layout.

You are adding/replacing fragment programatically so what is the need to have the same in xml.

Have this in activity_main.xml

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

    <EditText
        android:id="@+id/input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <FrameLayout
        android:id="@+id/container" // id is container
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/button1"
        android:layout_below="@+id/input"
      >
    </FrameLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/input"
        android:onClick="selectFrag"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:onClick="selectFrag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/input"
        android:layout_alignParentBottom="true"
        android:text="Button2" />

</RelativeLayout>

Then in MainActivity

   public class MainActivity extends Activity {
    Fragment fr; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void selectFrag(View view) {

        if(view == findViewById(R.id.button2)) {
            fr = new FragmentTwo();

        }
        else {
            fr = new FragmentOne();
        }
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.container, fr);
        fragmentTransaction.commit();
   }
}

FragmentOne

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.frag1, container, false);
        TextView warcraft=  (TextView) view.findViewById(R.id.moargold);
        EditText moargold = (EditText) getActivity().findViewById(R.id.input);
        Double vespenegas = Double.parseDouble(moargold.getText().toString());

        warcraft.setText(String.valueOf(vespenegas));
        Toast toast = Toast.makeText(getActivity(),String.valueOf(vespenegas) , Toast.LENGTH_SHORT);
        toast.show();
        return view;
    }
}

frag1.xml

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

             <TableLayout
                android:id="@+id/TableLayout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="1" >

                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/dis_moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Gold Count:" />

                    <TextView
                        android:id="@+id/moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />
                </TableRow>
 </TableLayout>
</LinearLayout>

FragmentTwo

public class FragmentTwo extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.frag2, container, false);
        EditText input = (EditText) getActivity().findViewById(R.id.input);



        TableLayout tl=(TableLayout) view.findViewById(R.id.TableLayout01);


        TableRow tr = new TableRow(getActivity());
        tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        TextView textview1 = new TextView(getActivity());
        textview1.setText("happy");
        tr.addView(textview1);

        TextView textview2 = new TextView(getActivity());
        textview2.setText("unhappy");
//###############To insert text from editview to table
//      Double buygas = Double.parseDouble(input.getText().toString());
//        textview2.setText(new Double(buygas).toString());
        tr.addView(textview2);

        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        return view;
    }
}

frag2.xml

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

             <TableLayout
                android:id="@+id/TableLayout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="1" >

                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/dis_moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Gold Count:" />

                    <TextView
                        android:id="@+id/moargold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />
                </TableRow>
 </TableLayout>
</LinearLayout>

Snap