天天看點

Activity擷取Fragment的值

利用的是接口回調功能

在Fragment裡要寫回調,具體代碼

package com.example.fragmentdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class Left extends Fragment {
    private TextView tv;
    @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 v = inflater.inflate(R.layout.fragment_left, container, false);
            tv = (TextView) v.findViewById(R.id.tv_left);
        return v;
    }
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
    //接口的作用就是把自己的值給外部用
    public void getTv(CallBack c){
        c.getText(tv.getText().toString());
    }
    
    //聲明回調接口
    public interface CallBack{
        public void getText(String msg);
    }

}      
<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="vertical"
    tools:context=".Left" >

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:id="@+id/tv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是左側的" />

    <Button
        android:id="@+id/btn_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點選擷取右側值" />

</LinearLayout>      

然後在Acitivity裡寫擷取Fragment的值

package com.example.fragmentdemo;

import com.example.fragmentdemo.Left.CallBack;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {
    private FragmentManager fm;
    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        final Left left = new Left();
        ft.add(R.id.left, left, "left");
        ft.commit();
        findViewById(R.id.right).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                left.getTv(new CallBack() {
                    
                    @Override
                    public void getText(String msg) {
                        Toast.makeText(MainActivity.this,msg,0).show();
                        
                    }
                });
                
            }
        });
    }

}      
<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:baselineAligned="false"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

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

    <Button
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" 
        android:text="擷取Fragment的值">
    </Button>

</LinearLayout>