Fragment传值
Activity向Fragment传值
Activity布局文件
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/fra"
android:layout_width="300dp"
android:layout_height="500dp"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
Activity具体代码
package com.example.day06;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText edv;
private Button but;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edv = (EditText) findViewById(R.id.edv);
but = (Button) findViewById(R.id.but);
final FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fra, new BlankFragment());
fragmentTransaction.commit();
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = edv.getText().toString();
BlankFragment blankFragment = new BlankFragment();
Bundle bundle = new Bundle();
bundle.putString("key", s);
blankFragment.setArguments(bundle);
FragmentManager supportFragmentManager1 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction1 = supportFragmentManager1.beginTransaction();
fragmentTransaction1.replace(R.id.fra, blankFragment);
fragmentTransaction1.commit();
}
});
}
}
Fragment布局文件
<?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 -->
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
Fragment具体代码
package com.example.day06;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class BlankFragment extends Fragment {
public BlankFragment() {
}
private TextView txt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_blank, null);
txt = inflate.findViewById(R.id.txt);
Bundle arguments = getArguments();
if (arguments != null) {
String key = (String) arguments.get("key");
txt.setText(key);
}
return inflate;
}
}
Fragment向Activity传值
Activity布局文件
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<TextView
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Activity具体代码
package com.example.day06;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity implements BlankFragment2.sendMessage {
private TextView txt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
txt2 = (TextView) findViewById(R.id.txt2);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.ll, new BlankFragment2());
fragmentTransaction.commit();
}
@Override
public void send(String text) {
txt2.setText(text);
}
}
Fragment布局文件
<?xml version="1.0" encoding="utf-8"?>
<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=".BlankFragment2">
<!-- TODO: Update blank fragment layout -->
<EditText
android:id="@+id/edi"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Fragment 具体代码
package com.example.day06;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.text.Editable;
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 {
private EditText edi;
private Button btn;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
sendMessage = (BlankFragment2.sendMessage) context;
}
public BlankFragment2() {
}
public interface sendMessage {
void send(String text);
}
public sendMessage sendMessage;
public void setSendMessage(BlankFragment2.sendMessage sendMessage) {
this.sendMessage = sendMessage;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_blank_fragment2, null);
edi = inflate.findViewById(R.id.edi);
btn = inflate.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = edi.getText().toString();
if (text != null) {
sendMessage.send(text);
}
}
});
return inflate;
}
}