gpt4 book ai didi

java - 使用 ActionBar.TabListener 将数据从一个选项卡传递到另一个选项卡

转载 作者:行者123 更新时间:2023-12-01 14:18:02 24 4
gpt4 key购买 nike

我有 mainactivity,其中包含两个选项卡并具有以下代码:

public class MainActivity extends Activity {
private class MyTabListener implements ActionBar.TabListener
{
private Fragment mFragment;
private final Activity mActivity;
private final String mFrag;

public MyTabListener( Activity activity, String fragName )
{
mActivity = activity;
mFrag = fragName;
}

@Override
public void onTabReselected( Tab tab, FragmentTransaction ft )
{
// TODO Auto-generated method stub

}

@Override
public void onTabSelected( Tab tab, FragmentTransaction ft )
{
mFragment = Fragment.instantiate( mActivity, mFrag );
ft.add( android.R.id.content, mFragment );
}

@Override
public void onTabUnselected( Tab tab, FragmentTransaction ft )
{
ft.remove( mFragment );
mFragment = null;
}
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


ActionBar ab = getActionBar();
ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );

Tab tab = ab.newTab()
.setText( "Current Trip" )
.setTabListener(
new MyTabListener( this, current.class.getName() ) );
ab.addTab( tab );

tab = ab.newTab()
.setText( "Display Result" )
.setTabListener(
new MyTabListener( this, display.class.getName() ) );
ab.addTab( tab );


File folder = new File(Environment.getExternalStorageDirectory() + "/tc");
boolean success = true;
if (!folder.exists()) {
//Toast.makeText(MainActivity.this, "Directory Does Not Exist, Create It", Toast.LENGTH_SHORT).show();
success = folder.mkdir();
}
if (success) {
//Toast.makeText(MainActivity.this, "Directory Created", Toast.LENGTH_SHORT).show();
} else {
//Toast.makeText(MainActivity.this, "Failed - Error", Toast.LENGTH_SHORT).show();
}
}
}

如何从 current.class 传递编辑文本的数据以进行显示并在 display.class 中用于计算?

最佳答案

我建议您使用共享首选项,例如

this is the frag that "writes":
private SharedPreferences prefs; // shared preferences
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("someId", "something"); //or you can use putInt, putBoolean ...
editor.commit();

this is the frag the "reads"
prefs = getActivity().getSharedPreferences("spa", Context.MODE_PRIVATE);
String someId=prefs.getString("someId",someId);

或者,您可以从一个 fragment 中调用另一个 fragment 中的 a 方法(最好通过 mainactivity 而不是直接执行此操作)。

 from frag 1:
((activity)getActivity()).somemethod();

in activity:

fragment2 fragment = (fragment2) getSupportFragmentManager().findFragmentByTag("fragment2");
fragment.somemethod();

关于java - 使用 ActionBar.TabListener 将数据从一个选项卡传递到另一个选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17932137/

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