文章目录
- 安卓学习,Activity与Fragment之间的通信(一)
- 1.从Fragment传值给Activity
- activity_main.xml
- Mainactivity.java
- BlankFragment.java
- fragment_blank.xml
- 2.从Activity传值给Fragment
- 在MainActivity中的重要代码
- 在Fragment中的重要部分
- 完整代码如下
- 使用接口回调方法来实现从Fragment传值给Activity
- Step1:在Fragment中定义一个回调接口(内部)
- Step2:在Fragment中写一个接口回调方法
- Step3:在Activity中重写实现这个接口中的方法
- 完整代码如下:
安卓学习,Activity与Fragment之间的通信(一)
1.从Fragment传值给Activity
这里就实现一个简单的例子来演示如何从Fragment中传信息出来给Activity
实际上就是用Fragment替换了一个Activity中的组件,并且当点击到Fragment中的按钮时,在fragment中调用Activity中的方法,将参数传过去。
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Mainactivity.java
Mainactivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
BlankFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment = new BlankFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.line1,fragment,null).commit();
}
public void receiveDataFragment(String data) {
Toast.makeText(this,"收到来自Fragment的数据 " + data, Toast.LENGTH_SHORT).show();
}
}
BlankFragment.java
BlankFragment.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class BlankFragment extends Fragment {
Button button;
private EditText editText;
private String content;
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
editText = view.findViewById(R.id.edit);
button = view.findViewById(R.id.tz);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
content = editText.getText().toString();
((MainActivity) getActivity()).receiveDataFragment(content);
}
});
}
}
fragment_blank.xml
fragment_blank.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".BlankFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<Button
android:id="@+id/tz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tz"
android:textSize="25dp"/>
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:hint="输入值"
/>
</LinearLayout>
</FrameLayout>
在Fragment中可以输入传递的值
button = view.findViewById(R.id.tz);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
content = editText.getText().toString();
((MainActivity) getActivity()).receiveDataFragment(content);
}
});
并且同时调用Activity中的方法
public void receiveDataFragment(String data) {
Toast.makeText(this,"收到来自Fragment的数据 " + data, Toast.LENGTH_SHORT).show();
}
就可以将信息传到activity
2.从Activity传值给Fragment
在MainActivity
中的重要代码
要想从Activity传值给Fragment也就是,先新建并定义出你要传给的Fragment,并且获取 FragmentManager
和 FragmentTransaction
,在创建Bundle
,并且将数据存放到Bundle中,最后再将用fragment的setArguments()
方法来将bundle设置到Fragment中。
//定义需要添加的Fragment
fragment = new BlankFragment2();
//获取FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
//获取FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//创建Bundle
//Bundle bundle;
bundle = new Bundle();
//添加默认数据到Bundle
bundle.putString("message", "Hello World! Hello My Life!");
//将默认数据设置到Fragment中
fragment.setArguments(bundle);
在Fragment中的重要部分
在Activity中设置好传给Fragment的数据后 (上面传出的值的Key为message,所以在获取数据时,我们选用的Key也应该同名),我们可以使用getArguments()
方法来获取bundle ,再使用bundle的getString()
方法来获取对应Key的值。
//通过bundle的getArguments来获取从Activity传来的数据
bundle = this.getArguments();
//获取对应key的值
message = bundle.getString("message");
完整代码如下
当不写值时,有传出一个默认的值,点击获取就可以获取到默认的值;还可以通过输入值,在点击传出,随后在fragment中点击获取就可以将值展示在EditText中。
activity_main3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity3"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edit_act"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:hint="输入值"
/>
<Button
android:id="@+id/cz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="将值传出"/>
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
fragment_blank2.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".BlankFragment2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<Button
android:id="@+id/btntz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取传入值"
android:textSize="25dp"/>
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:hint="等待传入值"
/>
</LinearLayout>
</FrameLayout>
MainActivity3.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity3 extends AppCompatActivity {
EditText editTextA;
BlankFragment2 fragment;
Bundle bundle;
private String content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
editTextA = findViewById(R.id.edit_act);
Button bcz = findViewById(R.id.cz);
//定义需要添加的Fragment
fragment = new BlankFragment2();
//获取FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
//获取FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//创建Bundle
//Bundle bundle;
bundle = new Bundle();
//添加默认数据到Bundle
bundle.putString("message", "Hello World! Hello My Life!");
//将默认数据设置到Fragment中
fragment.setArguments(bundle);
//这里就是动态传值
bcz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
content = editTextA.getText().toString();
bundle.putString("message", content);
fragment.setArguments(bundle);
}
});
fragmentTransaction.replace(R.id.line1,fragment,null).commit();
}
}
BlankFragment2.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class BlankFragment2 extends Fragment {
Button button;
EditText editText;
Bundle bundle;
String message;
public BlankFragment2() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank2, container, false);
editText = (EditText) view.findViewById(R.id.edit2);
button = (Button) view.findViewById(R.id.btntz);
//通过bundle的getArguments来获取从Activity传来的数据
bundle = this.getArguments();
//获取对应的值
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取对应key的值
message = bundle.getString("message");
editText.setText(message);
}
});
return view;
}
}
使用接口回调方法来实现从Fragment传值给Activity
要想实现这种方式的步骤如下:
Step1:在Fragment中定义一个回调接口(内部)
public interface CallBack{
public void getResult(String mes);
}
Step2:在Fragment中写一个接口回调方法
public void getData(CallBack callBack) {
callBack.getResult(meg);
}
Step3:在Activity中重写实现这个接口中的方法
fragment.getData(new BlankFragment3.CallBack() {
@Override
public void getResult(String mes) {
Toast.makeText(MainActivity4.this, mes, Toast.LENGTH_LONG).show();
editTextA.setText(mes);
}
});
完整代码如下:
activity_main4.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity4"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edit_act4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:hint="输入值"
/>
<Button
android:id="@+id/cz4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="将值传出"/>
<LinearLayout
android:id="@+id/line4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
fragment_blank3.xml
package com.example.myapplication;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class BlankFragment3 extends Fragment {
TextView textView ;
String meg;
public BlankFragment3() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank3, container, false);
textView = view.findViewById(R.id.text3);
meg = textView.getText().toString();
return view;
}
public interface CallBack{
public void getResult(String mes);
}
public void getData(CallBack callBack) {
callBack.getResult(meg);
}
}
MainActivity4.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity4 extends AppCompatActivity {
EditText editTextA;
BlankFragment3 fragment;
Bundle bundle;
private String content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
editTextA = findViewById(R.id.edit_act4);
Button bcz = findViewById(R.id.cz4);
fragment = new BlankFragment3();
//获取FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
//获取FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
bcz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragment.getData(new BlankFragment3.CallBack() {
@Override
public void getResult(String mes) {
Toast.makeText(MainActivity4.this, "-->>" + mes, Toast.LENGTH_LONG).show();
editTextA.setText(mes);
}
});
}
});
fragmentTransaction.replace(R.id.line4, fragment, null).commit();
}
}
BlankFragment3.java
package com.example.myapplication;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class BlankFragment3 extends Fragment {
TextView textView ;
String meg;
public BlankFragment3() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank3, container, false);
textView = view.findViewById(R.id.text3);
meg = textView.getText().toString();
return view;
}
public interface CallBack{
public void getResult(String mes);
}
public void getData(CallBack callBack) {
callBack.getResult(meg);
}
}