0

mainactivityは2つのタブを含み、次のコードを持っています:

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();
        }
    }
}

edittext のデータを から表示して計算current.classに使用するにはどうすればよいですか?display.class

4

1 に答える 1

1

たとえば、共有設定を使用することをお勧めします

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);

別の方法として、1 つのフラグメント内のメソッドを別のフラグメントから呼び出すこともできます (これは、直接ではなく mainactivity を介して行うのが最適です)。

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

 in activity:

 fragment2 fragment = (fragment2) getSupportFragmentManager().findFragmentByTag("fragment2");
 fragment.somemethod();
于 2013-07-29T19:12:00.440 に答える