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

Activity与Activity,Activity与Fragment,Fragment 与 Fragment之间值的传递

程序员文章站 2022-05-14 07:54:26
...

一、Activity与Activity之间值的传递

先创建两个活动,一个叫MainActivity,另一个叫SecondActivity

然后在activity_main.xml中添加一个Button,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:text="ToSecondActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toSecond"
        android:textAllCaps="false"/>

</LinearLayout>

activity_second.xml中添加一个TextView,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">



    <TextView
        android:id="@+id/showData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

然后分别在MainActivity和SecondActivity中初始化对应的控件

MainActivity代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button toSecond;

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

        initView();

    }

    private void initView() {

        toSecond = (Button)findViewById(R.id.toSecond);

        toSecond.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        
        switch (v.getId()){
            case R.id.toSecond:
                //跳转逻辑
                break;
        }

    }
}

SecondActivity代码如下:

public class SecondActivity extends AppCompatActivity {

    private TextView showData;

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

        initView();

        initData();

    }

    private void initView() {

        showData = (TextView)findViewById(R.id.showData);

    }

    private void initData(){

       //TODo
    }
}

第一种:通过setClass方法来实现​​

在MainActivity的click点击中添加如下代码:

case R.id.toSecond:
    //跳转逻辑
    Intent intent = new Intent(this,SecondActivity.class);
    intent.putExtra("name","一枚小垃圾");
    intent.putExtra("age","21");
    startActivity(intent);
    break;

在SecondActivity的

initData()方法中添加如下代码:

Intent intent = getIntent();
String name = intent.getStringExtra("name");
String age = intent.getStringExtra("age");
showData.setText("姓名:" + name + ";" + "年龄:" + age);

然后运行,我们看下效果:

Activity与Activity,Activity与Fragment,Fragment 与 Fragment之间值的传递

第二种:通过Bundle来实现:

修改MainActiviy中的case事件代码为:

case R.id.toSecond:
    //跳转逻辑
    Intent intent = new Intent(this,SecondActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("name","一枚小垃圾");
    bundle.putString("age","21");
    intent.putExtra("bundle",bundle);
    startActivity(intent);
    break;

然后再将SecondActivity中initData()方法中的代码修改为:

private void initData() {

    Intent intent = getIntent();
    Bundle bundleExtra = intent.getBundleExtra("bundle");
    String name = bundleExtra.getString("name");
    String age = bundleExtra.getString("age");
    showData.setText("姓名:" + name + ";" + "年龄:" + age);
}

下面我们再来看运行效果:

Activity与Activity,Activity与Fragment,Fragment 与 Fragment之间值的传递

我们可以看到运行效果是一样。那么有人要问了,intent和bundle传值有什么区别呢。这里就不赘述了,有兴趣的百度一下就行了。

二、Activity与Fragment之间值的传递

新建一个FirstFragment类,继承于Fragment.并重写onCreateView方法

再新建一个fragment_firset.xml布局文件。

直接上代码。

用Bundle传值

MainActivity完整代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button toSecond;
    private FragmentManager fm;
    private FragmentTransaction transaction;

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


        initView();


    }

    private void initView() {

        toSecond = (Button) findViewById(R.id.toSecond);

        toSecond.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        fm = getSupportFragmentManager();
        transaction = fm.beginTransaction();

        switch (v.getId()) {
            case R.id.toSecond:
                //跳转逻辑
                FirstFragment firstFragment = new FirstFragment();
                Bundle bundle = new Bundle();
                bundle.putString("name", "一枚小垃圾");
                bundle.putString("age", "22");
                firstFragment.setArguments(bundle);
                transaction.add(R.id.fragment, firstFragment);
                transaction.addToBackStack(null);
                break;
        }
        transaction.commit();
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:text="ToSecondActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toSecond"
        android:textAllCaps="false"/>

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"></FrameLayout>

</LinearLayout>

FirstFragment代码:

public class FirstFragment extends Fragment {

    private View view;
    private TextView fragment_showData;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_first, container, false);

        initView();

        initData();

        return view;
    }

    private void initData() {
        Bundle bundle = getArguments();
        String name = bundle.getString("name");
        String age = bundle.getString("age");
        fragment_showData.setText("name:" + name + ";"+ "age:" + age +"岁");
    }

    private void initView() {

        fragment_showData = (TextView) view.findViewById(R.id.fragment_showData);

    }
}

fragment_first.xml:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_showData"/>

</LinearLayout>

效果:

Activity与Activity,Activity与Fragment,Fragment 与 Fragment之间值的传递

二、Fragment与Fragment之间值的传递

fragment与fragment之间传值,总共分为Bundle传值以及通过接口回调之间的传值,由于博文篇幅太长了,请移步我的这篇博文。