(5)Fragment fragment传值个activity和fragment传值给fragment(终结篇) - 奇乐云's blog - 奇乐云的个人博客

会飞的鱼

奇乐云
首页 » Android » (5)Fragment fragment传值个activity和fragment传值给fragment(终结篇)

(5)Fragment fragment传值个activity和fragment传值给fragment(终结篇)

(1)创建3个视图XML窗口
[1]主activity窗口
<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <EditText
                android:id="@+id/userName_left"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/user" />

            <EditText
                android:id="@+id/password_left"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/pass" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linear_right"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal" />


    </LinearLayout>


    <LinearLayout
        android:id="@+id/linear_button"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" />
</LinearLayout>

[2]右边fragment窗口
<?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:background="#f00"
    android:orientation="vertical"
    tools:context=".MainActivity">
        <EditText
            android:id="@+id/userName_right"
            android:hint="@string/user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/password_right"
            android:hint="@string/pass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:textAllCaps="true"
            android:layout_height="wrap_content"
            android:text="传值给activity" />
</LinearLayout>

[3]底部fragment窗口
<?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:background="#0f0"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/userName_button"
        android:hint="@string/user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/password_button"
        android:hint="@string/pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_button"
        android:layout_width="match_parent"
        android:textAllCaps="true"
        android:layout_height="wrap_content"
        android:text="传值给fragment" />
</LinearLayout>

(3)创建3个子窗口的类
[1]主activity窗口类(引入2个fragment)
package com.example.administrator.fragment_two;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.linear_right,new RightFragment()).commit();
        getSupportFragmentManager().beginTransaction().replace(R.id.linear_button,new buttonFragment()).commit();
    }
}

[2]右边fragment窗口(传值给activity)
package com.example.administrator.fragment_two;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

//fragment 向activity中传值
public class RightFragment extends Fragment implements View.OnClickListener {

    private EditText username_right;
    private EditText password_right;
    private Button button;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragnment_right,container,false);
        username_right = view.findViewById(R.id.userName_right);
        password_right = view.findViewById(R.id.password_right);
        button = view.findViewById(R.id.btn);
        button.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        //点击按钮获得fragment中的edittext输入框中的内容
        String userNameRight = username_right.getText().toString();
        String passwordRight = password_right.getText().toString();
        //从fragment 获取activity对象
        MainActivity mainActivity = (MainActivity) getActivity();
        EditText userNameLeft = mainActivity.findViewById(R.id.userName_left);
        EditText passwordLeft = mainActivity.findViewById(R.id.password_left);
        userNameLeft.setText(userNameRight);
        passwordLeft.setText(passwordRight);
    }
}


[3]底部fragment窗口类(传值给右边fragment
package com.example.administrator.fragment_two;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class buttonFragment extends Fragment implements View.OnClickListener {

    private EditText username_button;
    private EditText password_button;
    private Button btn_button;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragnment_button,container,false);
        username_button = view.findViewById(R.id.userName_button);
        password_button = view.findViewById(R.id.password_button);
        btn_button = view.findViewById(R.id.btn_button);
        btn_button.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        //点击按钮获得fragment中的edittext输入框中的内容
        String userNameButton = username_button.getText().toString();
        String passwordButton = password_button.getText().toString();
        //从fragment获取fragment对象
        //首先获取碎片管理者
        FragmentManager fragmentManager = getFragmentManager();
        //从管理者中获取对象
        RightFragment buttonFragment = (RightFragment) fragmentManager.findFragmentById(R.id.linear_right);
        //获取底部fragment视图对象
        View view1 = buttonFragment.getView();
        //传入数据
        EditText userNameright = view1.findViewById(R.id.userName_right);
        EditText passwordright = view1.findViewById(R.id.password_right);
        userNameright.setText(userNameButton);
        passwordright.setText(passwordButton);
    }
}

(4)效果图
device-2018-12-27-175108.png

文章如无特别注明均为原创! 作者: 奇乐云, 转载或复制请以 超链接形式 并注明出处 奇乐云's blog - 奇乐云的个人博客
原文地址《 (5)Fragment fragment传值个activity和fragment传值给fragment(终结篇)》发布于2018-12-27

分享到:
打赏

评论

游客

切换注册

登录

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录
切换登录

注册