Fragment的使用可以让我们的应用更灵活的适配各种型号的安卓设备,但是对于Fragment和Activity之间的通信,很多朋友应该比较陌生,下面我们就通过一个实例来看一看如何实现。
一、Activity->Fragment传递数据
1.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:id="@+id/rl_fragment"
android:orientation="vertical"
android:layout_height="match_parent" >
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送" />
</LinearLayout>
2.fragment.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" >
<TextView
android:id="@+id/tv_fragment"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
3.MainActivity.java:
package com.example.fragementcommunication;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText mMainActivityET;
private Button mSendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mMainActivityET = (EditText) findViewById(R.id.et_input);
mSendButton = (Button) findViewById(R.id.btn_send);
mSendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text = mMainActivityET.getText().toString();
MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("input", text);
myFragment.setArguments(bundle);// 传递string
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.rl_fragment, myFragment, "myfragment");
transaction.commit();
Toast.makeText(MainActivity.this, "向Fragment发送数据" + text, Toast.LENGTH_SHORT).show();
}
});
}
}
4.MyFragment.java:
package com.example.fragementcommunication;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmet, null);
TextView mFragmentTextView = (TextView) view.findViewById(R.id.tv_fragment);
String string = getArguments().getString("input");// 获取数据;
mFragmentTextView.setText(string);
Toast.makeText(getActivity(), "成功获取数据" + string, Toast.LENGTH_SHORT).show();
return view;
}
}
总结:发送数据方法->setArguments(bundle)
接收数据方法->getArguments()
下面我们看一下效果:
二、Fragment向Activity传值(接口回调的方式)
布局文件和上面一样,我们看一下java代码的改变:
1.MainActivity.java:
package com.example.fragementcommunication;
import com.example.fragementcommunication.MyFragment.MyListener;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements MyListener{
private EditText mMainActivityET;
private Button mSendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mMainActivityET = (EditText) findViewById(R.id.et_input);
mSendButton = (Button) findViewById(R.id.btn_send);
mSendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text = mMainActivityET.getText().toString();
MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("input", text);
myFragment.setArguments(bundle);// 传递string
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.rl_fragment, myFragment, "myfragment");
transaction.commit();
Toast.makeText(MainActivity.this, "向Fragment发送数据" + text, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void callback(String back) {
Toast.makeText(MainActivity.this, "获取到从Fragment中传来的数据" + back, Toast.LENGTH_SHORT).show();
}
}
2.MyFragment.java:
package com.example.fragementcommunication;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyFragment extends Fragment {
public MyListener listener;
private String back="已经接收到数据!谢谢";
/**
* 通过接口回调的方式从Fragment向activity传值;
* @author Administrator
*
*/
public interface MyListener{
public void callback(String back);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
listener=(MyListener) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmet, null);
TextView mFragmentTextView = (TextView) view.findViewById(R.id.tv_fragment);
String string = getArguments().getString("input");// 获取数据;
mFragmentTextView.setText(string);
Toast.makeText(getActivity(), "成功获取数据" + string, Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "向Activity传递数据" + back, Toast.LENGTH_SHORT).show();
listener.callback(back);
return view;
}
}
运行如下:
这时我们可以看到先是MainActivity向Fragment传递了123,然后Fragment向Activity传递了”已经接收到数据!谢谢“。
喜欢的朋友请关注我和我的公众号!谢谢
![](https://i-blog.csdnimg.cn/blog_migrate/60ccd2962eb352ac1080a63790ddaad9.png)