Fragment Intermediate(III): Creating a activity that alternate between fragments onclick of respective button

后端 未结 2 564
孤城傲影
孤城傲影 2021-01-23 14:10

Aim:

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

Background:

Fragm

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-23 14:53

    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

    
    
        
    
            
        
    
        
        
    
        

    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

    
    
    
                 
    
                    
    
                        
    
                        
                    
     
    
    

    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

    
    
    
                 
    
                    
    
                        
    
                        
                    
     
    
    

    Snap

    enter image description here

提交回复
热议问题