zoukankan      html  css  js  c++  java
  • 14 fragment传值

    • 两个fragment传值

      方式一

      • 布局文件代码:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            tools:context=".MainActivity" >
        
            <!-- 静态  必须要写ID-->
        
            <fragment
                android:id="@+id/fragment1"
                android:name="com.qf.day14_fragment_4.fragment.MyFragment1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#0f0" />
            <!-- 动态 -->
        
            <LinearLayout
                android:id="@+id/ll_fragment_id"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" />
        
        </LinearLayout>
      • 布局文件逻辑代码:

        package com.qf.day14_fragment_4;
        
        import com.qf.day14_fragment_4.fragment.MyFragment2;
        
        import android.os.Bundle;
        import android.app.Activity;
        import android.app.FragmentManager;
        import android.app.FragmentTransaction;
        import android.view.Menu;
        
        public class MainActivity extends Activity {
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
                //管理者对象
                FragmentManager manager = getFragmentManager();
                FragmentTransaction transaction =   manager.beginTransaction();
        
                MyFragment2 myFragment2 = new MyFragment2();
        
                transaction.replace(R.id.ll_fragment_id,myFragment2);
        
                transaction.commit();
            }
        
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }
        
        }
        
      • 其中一个fragment逻辑代码:

        package com.qf.day14_fragment_4.fragment;
        
        import android.app.Fragment;
        import android.app.FragmentManager;
        import android.app.FragmentTransaction;
        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup;
        import android.widget.Button;
        
        import com.qf.day14_fragment_4.R;
        
        public class MyFragment1 extends Fragment{
        
            private Button btn;
        
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                View v = inflater.inflate(R.layout.fragment1_layout, container, false);
        
                btn = (Button) v.findViewById(R.id.btn);
                //点击按钮传值
                btn.setOnClickListener(new OnClickListener() {
        
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
        
                        //管理者对象
                        FragmentManager manager = getFragmentManager();
                        FragmentTransaction transaction =   manager.beginTransaction();
        
                        MyFragment2 myFragment2 = new MyFragment2();
        
                        Bundle bundle = new Bundle();
        
                        bundle.putString("msg", "左边Fragment向右边Fragment传值");
        
                        myFragment2.setArguments(bundle);
        
                        transaction.replace(R.id.ll_fragment_id, myFragment2);
        
                        transaction.commit();
                    }
                });
                return v;
            }
        }
        

      方式二 前提:必须两个静态fragment

      • 界面xml代码:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            tools:context=".MainActivity" >
        
            <fragment
                android:id="@+id/leftfragment"
                android:name="com.qf.day14_fragment_demo5.fragment.Fragment1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#00f" />
        
            <fragment
                android:id="@+id/rightfragment"
                android:name="com.qf.day14_fragment_demo5.fragment.Fragment2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        
        </LinearLayout>
      • 其中核心fragment代码

        package com.qf.day14_fragment_demo5.fragment;
        
        import android.app.Fragment;
        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;
        
        import com.qf.day14_fragment_demo5.R;
        
        public class Fragment1 extends Fragment{
        
            private EditText etContent;
            private Button btnSend;
        
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                // TODO Auto-generated method stub
        
                View view = inflater.inflate(R.layout.layout01, container, false);
                etContent = (EditText) view.findViewById(R.id.et_content);
                btnSend = (Button) view.findViewById(R.id.btn_send);
        
                btnSend.setOnClickListener(new OnClickListener() {
        
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
        
                        String msg = etContent.getText().toString().trim();
        
                        //通过Fragment的id  获取Fragment2的对象
        //              Fragment2 fragment2 = (Fragment2) getFragmentManager().findFragmentById(R.id.rightfragment);
        //              fragment2.setTextValues(msg);
        
                        //TextView tv = (TextView) getFragmentManager().findFragmentById(R.id.rightfragment).getView().findViewById(R.id.tv_show);
                        TextView tv = (TextView) getActivity().findViewById(R.id.tv_show);
                        tv.setText(msg);
        
                    }
        
                });
        
                return view;
            }
        
        
        }
        
    • fragment给界面传值(Activity) 接口回调

      • 界面代码

        package com.qf.day14_fragment_demo3;
        
        import com.qf.day14_fragment_demo3.callback1.CallBackValue;
        
        import android.os.Bundle;
        import android.annotation.SuppressLint;
        import android.app.Activity;
        import android.app.FragmentManager;
        import android.app.FragmentTransaction;
        import android.view.Menu;
        import android.widget.TextView;
        
        public class MainActivity extends Activity implements CallBackValue{
        
            private TextView tv;
        
            @SuppressLint("NewApi")
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
                tv = (TextView) findViewById(R.id.tv);
        
                FragmentManager  manager = getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.ll_fragment_id, new MyFragment());
                transaction.commit();
            }
        
            //回调接口的方法
            @Override
            public void sendMessage(String msg) {
                // TODO Auto-generated method stub
                tv.setText(msg);
            }
        
        
        
        }
        
      • 接口代码

        package com.qf.day14_fragment_demo3.callback1;
        
        public interface CallBackValue {
        
            public void sendMessage(String msg);
        
        }
        
      • fragment代码

        package com.qf.day14_fragment_demo3;
        
        import com.qf.day14_fragment_demo3.callback1.CallBackValue;
        
        import android.annotation.SuppressLint;
        import android.app.Activity;
        import android.app.Fragment;
        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup;
        import android.widget.Button;
        import android.widget.EditText;
        
        
        @SuppressLint("NewApi")
        public class MyFragment extends Fragment{
        
            CallBackValue callBackValue;
        
            private EditText et;
            private Button btnFTA;
        
            /**
             * 与Activity第一次链接是调用
             */
            @Override
            public void onAttach(Activity activity) {
                // TODO Auto-generated method stub
                super.onAttach(activity);
        
                //Fragment归属的Activity是getActivity
                callBackValue = (CallBackValue) getActivity();
            }
        
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                View v = inflater.inflate(R.layout.fragment_layout, container,false);
                et = (EditText) v.findViewById(R.id.et);
                btnFTA = (Button) v.findViewById(R.id.btn_FTA);
        
                btnFTA.setOnClickListener(new OnClickListener() {
        
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        //将值传给Activity
                        String msg = et.getText().toString().trim();
        
                        callBackValue.sendMessage(msg);
                    }
                });
        
        
                return v;
            }
        
        
        
        }
        
    • fragment 获取 界面数据(Activity)

      • 可以fragment中直接getActivity 或者getContext 获取其对象
      • 或者动态创建时用setAgunment 在fragment中getAgument
  • 相关阅读:
    Windows Server 2003服务器.net4.0+IIS6.0的服务器,IE11浏览器访问的不兼容性
    SVN标准目录结构
    关于SVN 目录结构,使用教程
    Visio编辑数据库模型列
    IIS 8.5配置.net网站[花了半个多小时]
    调试存储过程
    generate the next AttestationNumber, 格式是ICD-EPRG-DEV-0000000001,ICD-EPRG-DEV-0000000002
    构建布局良好的Windows程序
    初始window程序
    使用ADO.NET访问数据库查询和操作数据
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152253.html
Copyright © 2011-2022 走看看