Activity与Fragment数据传递之Activity从Fragment获取数据 分类: Android 2015-07-02 09:56 12人阅读 评论(0) 收藏

整理Fragment与Activity之间的数据交换,大体上包括三种:

1、Fragment从Activity获取数据

2、Activity从Fragment获取数据

3、Fragment之间获取数据

         通常,如果Activity向不同的Activity传递数据,最常用的是通过Intent.putExtra()方法,将简单类型的数据或可序列化的数据保存在Intent对象中,然后在目标Activity中使用getXxx(getInt,getString等)方法获得这些数据。 
从Activity向Fragment中传递数据可以Fragment.getArguments方法向Fragment传递参数值,并且通过Fragment.getArguments方法获取传递的参数值。 
       从Fragment向Actvity中传递数据采用接口回调的方式实现数据的传递。 本文介绍了第二种Fragment数据传递技术之Activity从Fragment获取数据的方法。

思路:把数据先保存到Bundle中,然后在调用setArguments()方法进行传递。

MainActivity.java代码:

<pre name="code" class="java">public class MainActivity extends FragmentActivity {  
  
    private FragmentManager manager;  
    private FragmentTransaction transaction;  
    private Button button;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        /* 获取对应的控件 */  
        button = (Button) this.findViewById(R.id.button);  
        /* 获取manager */  
        manager = this.getSupportFragmentManager();  
        /* 创建事物 */  
        transaction = manager.beginTransaction();  
  
        /* 创建LeftFragment(在内部类中使用到了,所以要用final) */  
        final LeftFragment leftFragment = new LeftFragment();  
        /* 把Fragment添加到对应的位置 */  
        transaction.add(R.id.left, leftFragment, "left");  
        /* 提交事物 */  
        transaction.commit();  
  
        /* 设置按钮的监听事件 */  
        button.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
  
                /* 使用接口回调的方法获取数据 */  
                leftFragment.getEditText(new CallBack() {  
  
                    @Override  
                    public void getResult(String result) {  
                        /*打印信息*/  
                        Toast.makeText(MainActivity.this, "-->>" + result, 1).show();  
                    }  
                });  
  
            }  
        });  
  
    }  
  
}  

LeftFragment.java代码:

<pre name="code" class="java">public class LeftFragment extends Fragment {  
  
    private Button button;  
    private EditText editText;  
  
    public LeftFragment() {  
        // TODO Auto-generated constructor stub  
    }  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
    }  
      
    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  
        /*动态加载布局*/  
        View view = inflater.inflate(R.layout.left, null);  
        /*从动态布局中获取对应的控件*/  
        editText = (EditText) view.findViewById(R.id.editText1);  
        return view;  
    }  
      
    @Override  
    public void onPause() {  
        // TODO Auto-generated method stub  
        super.onPause();  
    }  
      
    /*接口回调*/  
    public void getEditText(CallBack callBack){  
        /*获取文本框的信息*/  
        String msg = editText.getText().toString();  
        callBack.getResult(msg);  
    }  
      
    /*接口*/  
    public interface CallBack{  
        /*定义一个获取信息的方法*/  
        public void getResult(String result);  
    }  
}  

activity_main.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" >  
  
    <LinearLayout  
        android:id="@+id/left"  
        android:layout_width="224dp"  
        android:layout_height="match_parent"  
        android:background="#CCCCCC"  
        android:orientation="vertical" >  
    </LinearLayout>  
  
    <LinearLayout  
        android:layout_width="wrap_content"  
        android:layout_height="match_parent"  
        android:orientation="vertical" >  
  
        <Button  
            android:id="@+id/button"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="获得Fragment的值" />  
    </LinearLayout>  
  
</LinearLayout> 

left.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="vertical" >  
  
    <EditText  
        android:id="@+id/editText1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:ems="10" >  
  
        <requestFocus />  
    </EditText>  
  
</LinearLayout>  

参考源码(接口回调方法):

http://download.csdn.net/detail/u010963246/8861655

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/xieping/p/4666310.html