一、Fragment传值到Actvity:
activity_main.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"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearlayout_left"
android:layout_weight="1"
>
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.fragmentpassvalueactivity.fragments.OneFragment"
android:id="@+id/fragment"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearlayout_right">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="等待Fragment传值过来"
android:textSize="20sp"
android:id="@+id/textView_name"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
|
fragment_one.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_userName"
android:hint="请输入用户名"
android:textSize="20sp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="传值到Activity"
android:textSize="20sp"
android:id="@+id/button_pass"
android:layout_gravity="center_horizontal" />
</LinearLayout>
|
OneFragment.java中:
package com.example.fragmentpassvalueactivity.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.example.fragmentpassvalueactivity.R;
import com.example.fragmentpassvalueactivity.domain.PassValue;
public class OneFragment extends Fragment {
private EditText editText_userName;
private Button button_pass;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
this.editText_userName= (EditText) view.findViewById(R.id.editText_userName);
this.button_pass= (Button) view.findViewById(R.id.button_pass);
this.button_pass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userName=editText_userName.getText().toString();
if(TextUtils.isEmpty(userName)){
editText_userName.setError("用户名不能为空!");
editText_userName.requestFocus();
return;
}
//方式一:根据findViewById查找控件并赋值
//MainActivity mainActivity= (MainActivity) getActivity();
//TextView textView_name= (TextView) mainActivity.findViewById(R.id.textView_name);
//textView_name.setText(userName);
//方式二:根据setter方法传值
//MainActivity mainActivity= (MainActivity) getActivity();
//mainActivity.setName(userName);
//方式三:通过让Activity实现接口的方式来传值
PassValue passValue= (PassValue) getActivity();
passValue.setName(userName);
}
});
return view;
}
}
|
PassValue接口中:
package com.example.fragmentpassvalueactivity.domain;
public interface PassValue {
void setName(String name);
} |
MainActivity.java中:
package com.example.fragmentpassvalueactivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.fragmentpassvalueactivity.domain.PassValue;
public class MainActivity extends AppCompatActivity implements PassValue{
private TextView textView_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.textView_name= (TextView) this.findViewById(R.id.textView_name);
}
@Override
public void setName(String name) {
this.textView_name.setText(name);
}
}
|
二、Activity传值到Fragment:
方式一:通过给fragment添加setter函数传值到 fragment
activity_main.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"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/linearlayout_left"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:textSize="20sp"
android:id="@+id/editText_username" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="传值"
android:textSize="20sp"
android:onClick="add"
android:id="@+id/button_pass"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_weight="1"
android:id="@+id/linearlayout_right"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.activitypassvaluefragment.fragments.OneFragment"
android:id="@+id/fragment"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
|
fragment_one.xml中:
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="等待Activity传值过来"
android:id="@+id/textView_name"
android:textSize="30sp"
android:gravity="center"
/>
</LinearLayout>
|
OneFragment.java中:
package com.example.activitypassvaluefragment.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.example.activitypassvaluefragment.R;
public class OneFragment extends Fragment {
private TextView textView_name;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
this.textView_name= (TextView) view.findViewById(R.id.textView_name);
return view;
}
public void setName(String userName) {
textView_name.setText(userName);
}
}
|
MainActivity.java中:
package com.example.activitypassvaluefragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.example.activitypassvaluefragment.fragments.OneFragment;
public class MainActivity extends AppCompatActivity {
private FragmentManager fragmentManager;
private EditText editText_username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.fragmentManager=this.getFragmentManager();
this.editText_username= (EditText) this.findViewById(R.id.editText_username);
}
public void add(View view){
String userName=this.editText_username.getText().toString();
if(TextUtils.isEmpty(userName)){
this.editText_username.setError("用户名不能为空");
this.editText_username.requestFocus();
return;
}
//根据Fragment对象id查找Fragment
OneFragment oneFragment= (OneFragment) fragmentManager.findFragmentById(R.id.fragment);
oneFragment.setName(userName);
}
}
|
方式二:oneFragment.setArguments(bundle);
activity_main.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"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/linearlayout_left"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:textSize="20sp"
android:id="@+id/editText_username" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="传值"
android:textSize="20sp"
android:onClick="add"
android:id="@+id/button_pass"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_weight="1"
android:id="@+id/linearlayout_right"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
|
fragment_one.xml中:
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="等待Activity传值过来"
android:id="@+id/textView_name"
android:textSize="30sp"
android:gravity="center"
/>
</LinearLayout>
|
OneFragment.java中:
package com.example.activitypassvaluefragment.fragments;
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 com.example.activitypassvaluefragment.R;
public class OneFragment_bak01 extends Fragment {
private TextView textView_name;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle=this.getArguments();
String name=bundle.getString("userName");
View view=inflater.inflate(R.layout.fragment_one, container, false);
this.textView_name= (TextView) view.findViewById(R.id.textView_name);
this.textView_name.setText(name);
return view;
}
}
|
MainActivity.java中:
package com.example.activitypassvaluefragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import com.example.activitypassvaluefragment.fragments.OneFragment;
public class MainActivity_bak01 extends AppCompatActivity {
private FragmentManager fragmentManager;
private EditText editText_username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.fragmentManager=this.getFragmentManager();
this.editText_username= (EditText) this.findViewById(R.id.editText_username);
}
public void add(View view){
String userName=this.editText_username.getText().toString();
if(TextUtils.isEmpty(userName)){
this.editText_username.setError("用户名不能为空");
this.editText_username.requestFocus();
return;
}
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
OneFragment oneFragment=new OneFragment();
Bundle bundle=new Bundle();
bundle.putString("userName",userName);
oneFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.linearlayout_right,oneFragment);
fragmentTransaction.commit();
}
} |
方式三:通过FragmentManager查找添加过的Fragment并通过调用 fragment.getView()的View对象进而查找控件并赋值
activity_main.xml、fragment_one.xml和方式二的activity_main.xml、fragment_one.xml一样
OneFragment.java中:
package com.example.activitypassvaluefragment.fragments;
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 com.example.activitypassvaluefragment.R;
public class OneFragment_bak02 extends Fragment {
private TextView textView_name;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
this.textView_name= (TextView) view.findViewById(R.id.textView_name);
return view;
}
}
|
MainActivity.java中:
package com.example.activitypassvaluefragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.example.activitypassvaluefragment.fragments.OneFragment;
public class MainActivity_bak02 extends AppCompatActivity {
private FragmentManager fragmentManager;
private EditText editText_username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.fragmentManager=this.getFragmentManager();
this.editText_username= (EditText) this.findViewById(R.id.editText_username);
}
public void add(View view){
String userName=this.editText_username.getText().toString();
if(TextUtils.isEmpty(userName)){
this.editText_username.setError("用户名不能为空");
this.editText_username.requestFocus();
return;
}
//根据Fragment对象id查找Fragment
OneFragment oneFragment= (OneFragment) fragmentManager.findFragmentById(R.id.fragment);
//得到Fragment对象关联的View对象
View view_fragment=oneFragment.getView();
//查找Fragment控件并赋值
TextView textView_name= (TextView) view_fragment.findViewById(R.id.textView_name);
textView_name.setText(userName);
}
}
|