利用的是接口回调功能
在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>