gpt4 book ai didi

android - 将字符串从 fragment 主要 Activity 传递到 viewpager 中的 fragment Activity

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:10 26 4
gpt4 key购买 nike

我想将一个字符串传递给 fragment Activity (主要)中的所有 fragment (子),也许这张图片可以解释我到底想做什么

https://dl.dropboxusercontent.com/u/57465028/SC20140205-163325.png

所以,从上图看...我想通过按下按钮将字符串从 edittext 传递到 viewpager 中的所有 Activity ...我该怎么做?

我尝试遵循此代码 https://stackoverflow.com/a/12739968/2003393但它不能解决我的问题..

请帮助我...我被卡住了

提前致谢。

这是我的 fragment Activity (MainActivity) 的代码

公共(public)类 Swipe_Menu 扩展了 FragmentActivity {

//String KeyWord;


//private static final String KEYWORD = "keyword";

private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.swipe_menu_image);


Button Back = (Button)findViewById(R.id.account);
ImageButton Search = (ImageButton)findViewById(R.id.search);
EditText Keyword = (EditText)findViewById(R.id.keyword);

final String KeyWord = Keyword.getText().toString();


/**
* Back button click event
* */
Back.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

finish();
}
});



setUpView();
setTab();
}

protected void sendValueToFragments(String value) {
// it has to be the same name as in the fragment
Intent intent = new Intent("my_package.action.UI_UPDATE");
intent.putExtra("UI_KEY", KeyWord );
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

这是我的 fragment ( child Activity )

public class Store_Swipe extends Fragment {

public static final String ACTION_INTENT = "my_package.action.UI_UPDATE";

String KeyWord;
private TextView kata_keyword;

protected BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_INTENT.equals(intent.getAction())) {
String value = intent.getStringExtra("UI_KEY");
updateUIOnReceiverValue(value);
}
}
};

private void updateUIOnReceiverValue(String value) {
// you probably want this:
KeyWord = value;
}

public static Fragment newInstance(Context context) {
Store_Swipe f = new Store_Swipe();
return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION_INTENT);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);
}

@Override
public void onDestroy() {
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
super.onDestroy();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/*Bundle bundle = this.getArguments();
KeyWord = bundle.getString("keyword");*/
View view = inflater.inflate(R.layout.store_swipe, container, false);
init(view);

return view;

}

void init(View view) {
kata_keyword = (TextView) view.findViewById(R.id.keyword);
//ImageView image = (ImageView) view.findViewById(R.id.image_error);
kata_keyword.setText(KeyWord);
}

最佳答案

您无法直接访问驻留在 ViewPager 中的 fragment ,因此您无法直接引用它们。

在这些情况下,我所做的是从 Activity 向 Fragments 发送广播消息。为此,在 fragment 中注册一个 BroadcatReceiver(在 onCreateonCreateView 中 - 您的决定)m,为该接收器设置自定义操作(例如 "my_package.actions.internal.BROADCAST_ACTION"),不要忘记从补充方法中注销接收器。

当你想从 Activity 中发送消息时,使用上述操作创建一个 Intent ,在 Intent 额外添加字符串并发送广播。

在你的接收者的 onReceive 方法中(在 fragment 中),从 intent 参数中获取字符串,然后你就有了字符串。

有道理吗?

编辑:为了提供一些代码,下面是我要对 fragment 所做的更改:

public class Store_Swipe extends Fragment {

public static final String ACTION_INTENT = "my_package.action.UI_UPDATE";

protected BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_INTENT.equals(intent.getAction())) {
String value = intent.getStringExtra("UI_KEY");
updateUIOnReceiverValue(value);
}
}
};

private void updateUIOnReceiverValue(String value) {
// you probably want this:
kata_keyword.setText(value);
}

String KeyWord;
private TextView kata_keyword;

public static Fragment newInstance(Context context) {
Store_Swipe f = new Store_Swipe();
return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION_INTENT);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);
}

@Override
public void onDestroy() {
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
super.onDestroy();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle bundle = this.getArguments();
KeyWord = bundle.getString("keyword");
View view = inflater.inflate(R.layout.store_swipe, container, false);
init(view);

return view;

}

void init(View view) {
kata_keyword = (TextView) view.findViewById(R.id.keyword);
ImageView image = (ImageView) view.findViewById(R.id.image_error);
kata_keyword.setText(KeyWord);
}
}

我将从 Activity 中获得的代码,参数是来自 EditText 的值:

protected void sendValueToFragments(String value) {
// it has to be the same name as in the fragment
Intent intent = new Intent("my_package.action.UI_UPDATE");
intent.putExtra("UI_KEY", value);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

您可以从在 onCreate 中设置的点击监听器中调用它:

findViewById(R.id.button_id).setOnClickListener(new OnClickListener() {            
@Override
public void onClick(View v) {
String valueThatYouWantToSend = null; /// just the value
sendValueToFragments(valueThatYouWantToSend);
}
});

关于android - 将字符串从 fragment 主要 Activity 传递到 viewpager 中的 fragment Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21573810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com