第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

3-AVI--Activity與Fragment的數(shù)據(jù)傳遞

標(biāo)簽:
JavaScript

一、Activity向Fragment传数据效果:

webp

ac2fg.gif

1.Ac2FgActivity.java
public class Ac2FgActivity extends AppCompatActivity {    @BindView(R.id.tv_data_content)
    EditText mTvDataContent;    @BindView(R.id.btn_send_data)
    Button mBtnSendData;    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ac2fg);
        ButterKnife.bind(this);
        initFragment();

    }    private void initFragment() {
        FragmentManager fm = getFragmentManager();//1.获取FragmentManager
        FragmentTransaction ft = fm.beginTransaction();//2.fm开启事务
        //3.动态添加 (控件id,fragment对象,tag)
        ft.add(R.id.fl_fragmemt_content, new ResultFragment());
        ft.commit();//4.提交事务
    }    @OnClick(R.id.btn_send_data)    public void onViewClicked() {

        String result = mTvDataContent.getText().toString().trim();//获取输入结果
        ResultFragment rfg = new ResultFragment();//创建ResultFragment对象
        Bundle bundle = new Bundle();//创建Bundle对象
        bundle.putString("data", result);//为bundle赋值
        rfg.setArguments(bundle);//为ResultFragment设置Arguments
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fl_fragmemt_content, rfg);//替换之前的
        ft.commit();
    }
}
2.activity_ac2fg.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/tv_data_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="I'm Activity:请输入传入fragment的信息"/>

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_data_content"
        android:layout_centerHorizontal="true"
        android:text="点击传值"/>

    <FrameLayout
        android:id="@+id/fl_fragmemt_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/btn_send_data">
    </FrameLayout></RelativeLayout>
3.ResultFragment.java
public class ResultFragment extends Fragment {    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content, container, false);
        TextView f_tv_content = view.findViewById(R.id.f_tv_content);
        Bundle bundle = getArguments();        if (bundle != null) {
            String reslut = bundle.getString("data");
            f_tv_content.setText(reslut);
        }        return view;
    }
}
4.fragment_content.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">    <TextView
        android:id="@+id/f_tv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="#D5DBDD"
        android:text="I'm Fragment"
        android:textSize="24dp"/></RelativeLayout>

webp

Activity向Fragment传数据.png



二、Fragment向Activity传数据效果:

webp

fg2ac.gif

1.Fg2AcActivity.java
public class Fg2AcActivity extends AppCompatActivity{    @BindView(R.id.activity_tv_content)
    TextView mActivityTvContent;    @BindView(R.id.fl_fragmemt_send)
    FrameLayout mFlFragmemtSend;    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fg2ac);
        ButterKnife.bind(this);
        initFragment();

    }    private void initFragment() {
        FragmentManager fm = getFragmentManager();//1.获取FragmentManager
        FragmentTransaction ft = fm.beginTransaction();//2.fm开启事务
        //3.动态添加 (控件id,fragment对象,tag)
        ft.add(R.id.fl_fragmemt_send, new SendFragment());
        ft.commit();//4.提交事务
    }    /**
     * 设置数据方法
     * @param str
     */
    public void setDate(String str) {        if (str != null && !"".equals(str)) {
            mActivityTvContent.setText(str);
        }
    }
}
2.activity_fg2ac.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/activity_tv_content"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="I'm Activity:"
        android:textSize="24dp"/>

    <FrameLayout
        android:id="@+id/fl_fragmemt_send"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/activity_tv_content">
    </FrameLayout></RelativeLayout>
3.SendFragment.java
public class SendFragment extends Fragment {    private EditText tv_data_content;    private Button btn_send_data;    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_send, container, false);        //可实现控件事件
        tv_data_content = view.findViewById(R.id.tv_data_content);
        btn_send_data = view.findViewById(R.id.btn_send_data);
        btn_send_data.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View v) {
                String result = tv_data_content.getText().toString().trim();
                ((Fg2AcActivity)getActivity()).setDate(result);
            }
        });        return view;
    }
}

4.fragment_send

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:background="@color/qq_line"
                android:layout_height="match_parent">

    <EditText
        android:id="@+id/tv_data_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="I'm Fragment:请输入传入Activity的信息"/>

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_data_content"
        android:layout_centerHorizontal="true"
        android:text="点击传值"/></RelativeLayout>

webp

Fragment向Activity传数据.png



三、Fragment向Fragment传数据据效果:

webp

fg2fg.gif

1.Fg2FgActivity.java
public class Fg2FgActivity extends AppCompatActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fg2fg);
        initFragment();

    }    private void initFragment() {
        FragmentManager fm = getFragmentManager();//1.获取FragmentManager
        FragmentTransaction ft = fm.beginTransaction();//2.fm开启事务
        //3.动态添加 (控件id,fragment对象,tag)
        ft.add(R.id.f_left, new LeftFragment());
        ft.add(R.id.f_right, new RightFragment());
        ft.commit();//4.提交事务
    }
}
2.activity_fg2fg.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="horizontal">

    <FrameLayout
        android:id="@+id/f_left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/f_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout></LinearLayout>
3.LeftFragment.java
public class LeftFragment extends Fragment {
    EditText mTvDataContent;
    Button mBtnSendData;    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_left, container, false);
        mTvDataContent = view.findViewById(R.id.tv_data_content);
        mBtnSendData = view.findViewById(R.id.btn_send_data);

        mBtnSendData.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View v) {
                String str = mTvDataContent.getText().toString().trim();
                TextView mtv = getActivity().findViewById(R.id.right_content);
                mtv.setText(str);
            }
        });        return view;
    }
}
4.fragment_left.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <EditText
        android:id="@+id/tv_data_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="I'm left fragment"/>

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_data_content"
        android:layout_centerHorizontal="true"
        android:text="点击传值"/></LinearLayout>
5.RightFragment.java
public class RightFragment extends Fragment {    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_right, container, false);        return view;
    }
}
6.fragment_right.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#eee"
              android:orientation="vertical"><TextView
    android:id="@+id/right_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="I'm right fragment"/></LinearLayout>



作者:张风捷特烈
链接:https://www.jianshu.com/p/12ec3379de97


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消