Fragment Intermediate(III):创建一个在片段之间切换的活动,点击相应的按钮

时间:2014-04-03 09:26:15

标签: android android-fragments android-tablelayout

目的:

创建一个具有两个按钮的活动,按钮1和按钮2.单击时,片段将在两个片段之间交替。

背景:

Fragmentone将从主要活动中获取edittext,并在片段上的textview上获取settext。 Fragmenttwo将显示文本显示到tablelayout。 (我的最终目标是使用主要活动的输入并将其显示在表格的一个文本视图中,但它不起作用,请提出建议)## issue 1

问题:

应该显示fragmentone的button1不起作用。在将输入键入mainactivity的edittext之后,当我单击按钮时,fragmenttwo仍然存在。 FragmentOne没出来。在fragmentone中放一个吐司,toast显示我的输入。

特别感谢: 我想特别感谢这个社区中有帮助的人为我指导这一点。并且还要感谢Raghunandan,他指导我完成片段推进I,II。非常感谢您协助我完成学习之旅。希望我的问题和raghunandan的建议有助于减少那些在这次旅程中遇到困难的学习曲线。

片段中间体(I)

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

片段中间体(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>

2 个答案:

答案 0 :(得分:3)

FrameLayout中只有EditTextactivity_main.xmlFrameLayout是您添加或替换片段的容器(ViewGroup)。您的TableLayout可以采用片段布局。

您正在以编程方式添加/替换片段,因此需要在xml中使用相同的内容。

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

然后在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>

对齐

enter image description here

答案 1 :(得分:2)

在动态添加Fragments时,您可以将FragmentTwo保留在布局中。尝试在布局中删除它:

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

此外,在您的代码中,您动态添加片段并使用replace方法,将容器设置为标识fragment_place

fragmentTransaction.replace(R.id.fragment_place, fr);  

但是,此ID正在使用您的片段(FragmentTwo,上面的第一段代码),并且您无法替换未动态添加的片段。您需要在FrameLayout内托管您的片段,但您的片段没有ID,请尝试添加:

<FrameLayout
    android:id="@+id/fragment_place"     /// this line to create and id
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >  

然后,您可以根据需要将片段托管,添加和替换到framelayout中。