Android 导航抽屉中碎片之间的通信
我试图在点击按钮时将数据从片段A发送到导航抽屉的片段B。我尝试了捆绑和意图,但两者都不起作用 在片段A中,我有editText和按钮,当我单击时,数据被传递到另一个片段Android 导航抽屉中碎片之间的通信,android,android-fragments,Android,Android Fragments,我试图在点击按钮时将数据从片段A发送到导航抽屉的片段B。我尝试了捆绑和意图,但两者都不起作用 在片段A中,我有editText和按钮,当我单击时,数据被传递到另一个片段 在片段B中有一个文本视图,其中将显示editText数据,但我无法在导航抽屉中的片段之间进行通信让您的活动来进行通信 //Put the value YourNewFragment ldf = new YourNewFragment (); Bundle args = new Bundle(); args.putString("
在片段B中有一个文本视图,其中将显示editText数据,但我无法在导航抽屉中的片段之间进行通信让您的
活动来进行通信
//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("KEY", "VALUE");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
In onCreateView of the new Fragment:
//Retrieve the value
String value = getArguments().getString("KEY");
从控制导航抽屉的片段的位置,在main活动
中获取一个public static
变量。单击FragmentA
中的按钮时,将EditText
中的值存储到MainActivity
的public static
变量中。然后,当加载FragmentB
时,检查公共静态
值是否为空。如果不是null
将该变量的值放置到所需位置
这不是一种在片段之间传递值的优雅方式,但在您的情况下,它会很好地工作
如果您正在寻找如何将值从一个片段传递到另一个片段,请尝试以下方法
// To pass some value from FragmentA
FragmentB mFragmentB = new FragmentB();
Bundle args = new Bundle();
args.putString("VALUE", value);
mFragmentB.setArguments(args);
并从片段b
中使用代码获得传递的值
Bundle args = getArguments();
int value = args.getString("VALUE");
让您的活动
进行沟通
从控制导航抽屉的片段的位置,在main活动
中获取一个public static
变量。单击FragmentA
中的按钮时,将EditText
中的值存储到MainActivity
的public static
变量中。然后,当加载FragmentB
时,检查公共静态
值是否为空。如果不是null
将该变量的值放置到所需位置
这不是一种在片段之间传递值的优雅方式,但在您的情况下,它会很好地工作
如果您正在寻找如何将值从一个片段传递到另一个片段,请尝试以下方法
// To pass some value from FragmentA
FragmentB mFragmentB = new FragmentB();
Bundle args = new Bundle();
args.putString("VALUE", value);
mFragmentB.setArguments(args);
并从片段b
中使用代码获得传递的值
Bundle args = getArguments();
int value = args.getString("VALUE");
1-创建应用程序类
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
private static MyApplication mInstance;
public static synchronized MyApplication getInstance() {
return mInstance;
}
String mytext;
public String getMytext() {
return mytext;
}
public void setMytext(String mytext) {
this.mytext = mytext;
}
}
2-manifast中的应用程序名称标签
<application
android:name=".MyApplication"
.......
4-来自其他片段
MyApplication.getInstance().setMytext("your text here");
String text=MyApplication.getInstance().getMytext();
1-创建应用程序类
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
private static MyApplication mInstance;
public static synchronized MyApplication getInstance() {
return mInstance;
}
String mytext;
public String getMytext() {
return mytext;
}
public void setMytext(String mytext) {
this.mytext = mytext;
}
}
2-manifast中的应用程序名称标签
<application
android:name=".MyApplication"
.......
4-来自其他片段
MyApplication.getInstance().setMytext("your text here");
String text=MyApplication.getInstance().getMytext();
从第一个碎片启动碎片时
Bundle bundle = new Bundle();
bundle.putString("key", YOUR_EDITVIEW_TEXT);
Fragment fragment = new SECONDFragment();
if (arguments != null) {
fragment.setArguments(arguments);
}
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack("");
ft.commit();
在SecondFragment
private String mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mData = getArguments().getString("key");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.YourLayout, container, false);
TextView text = (TextView) rootView.findViewById(R.id.yourTextView);
text.setText(mData);
return rootView;
}
从第一个碎片启动碎片时
Bundle bundle = new Bundle();
bundle.putString("key", YOUR_EDITVIEW_TEXT);
Fragment fragment = new SECONDFragment();
if (arguments != null) {
fragment.setArguments(arguments);
}
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack("");
ft.commit();
在SecondFragment
private String mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mData = getArguments().getString("key");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.YourLayout, container, false);
TextView text = (TextView) rootView.findViewById(R.id.yourTextView);
text.setText(mData);
return rootView;
}
到目前为止你都试过了。发布你的代码。我认为你需要使用这个界面来检查youtube上的slidenerd在他的片段教程中你会得到一些东西发布你的代码和logcat。。。。。请参阅到目前为止尝试的堆栈溢出问题的答案。发布你的代码。我认为你需要使用这个界面来检查youtube上的slidenerd在他的片段教程中你会得到一些东西发布你的代码和logcat。。。。。请参阅有关堆栈溢出的答案