欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

安卓在同一个Activity中实现fragment的切换

程序员文章站 2022-05-05 14:22:50
...

一个简单的计算器程序

MainActivity:

package com.hdu.a15041929.homework3;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;


public class MainActivity extends FragmentActivity {

    private int a;
    private int b;
    private int ans;

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

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        if (fragment == null) {
            FragmentTransaction ft = fm.beginTransaction();

            fragment = new Fragment1();
            ft.add(R.id.fragmentContainer,fragment,"f1");

         /*   fragment = new Fragment2();
            ft.add(R.id.fragmentContainer,fragment,"f2");
            ft.hide(fragment);*/

            ft.commit();
        }
    }

    public void ShowFragment(int index){
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment;
        FragmentTransaction ft = fm.beginTransaction();
        if (index==1){
            fragment = fm.findFragmentByTag("f1");
            ft.show(fragment);
            fragment = new Fragment2();
            ft.add(R.id.fragmentContainer,fragment,"f2");
            ft.hide(fragment);
        }else{
            fragment = new Fragment2();
            ft.add(R.id.fragmentContainer,fragment,"f2");
            fragment = fm.findFragmentByTag("f1");
            ft.hide(fragment);
        }
        ft.commit();
    }

    public void SetData(int a, int b, int ans){
        this.a = a;
        this.b = b;
        this.ans = ans;
    }

    public int Geta(){
       return a;
    }
    public int Getb(){
        return b;
    }
    public int Getans(){
        return ans;
    }
}

Fragment1:

package com.hdu.a15041929.homework3;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


/**
 * Created by TS on 2017/11/21.
 */

public class Fragment1 extends Fragment{

    private EditText tv1;
    private EditText tv2;
    private int a;
    private int b;
    private int answer;
    private Button confirm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1,container,false);
        confirm = (Button)view.findViewById(R.id.button);
        tv1=(EditText) view.findViewById(R.id.num1);
        tv2=(EditText) view.findViewById(R.id.num2);
        Log.i("传输","1");
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                a = Integer.valueOf(tv1.getText().toString());
                b = Integer.valueOf(tv2.getText().toString());
                answer = a * b;
                Log.i("传输",String.valueOf(a));
                ((MainActivity)getActivity()).SetData(a,b,answer);

                ((MainActivity)getActivity()).ShowFragment(2);
            }
        });
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}


Fragment2:

package com.hdu.a15041929.homework3;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by TS on 2017/11/23.
 */

public class Fragment2 extends Fragment {
    private TextView text;
    private int a;
    private int b;
    private int ans;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2, container, false);
        text = (TextView)view.findViewById(R.id.textView2);
        a = ((MainActivity)getActivity()).Geta();
        b = ((MainActivity)getActivity()).Getb();
        ans = ((MainActivity)getActivity()).Getans();
        Log.i("传输","2");
        Log.i("传输",String.valueOf(a));
        text.setText(a+" * "+b+" 的结果是 "+ans);
        return view;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}
布局文件就是两个界面fragment和一个空白的activity_fragment。



相关标签: android