1

我想将一个从一个选项卡传递到另一个选项卡的意图。当我在第一个片段中按下一个按钮时,我希望它引导我到另一个片段(例如活动)。是否有另一种方法可以在类似于 startActivity(new Intent(EntryTab.this, Journals.class)); 的片段中执行此操作 ? 下面是我的代码,startActivity 行显示错误。请帮忙!

public class EntryTab extends Fragment{
    public static final String LOGTAG = "BALANCE";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.entries, container, false);
        Button save = (Button) ll.findViewById(R.id.entriesButton);
        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                BalanceApp myapp= (BalanceApp) getActivity().getApplicationContext();
                DataSource datasource = myapp.datasource;
                datasource.open();

                Log.i(LOGTAG,"inside tab entry");

                EditText amount = (EditText) ll.findViewById(R.id.amount);
                int amt = Integer.parseInt(amount.getText().toString());

                 EditText debit = (EditText)ll.findViewById(R.id.debitText);
                 String dr = debit.getText().toString();

                 EditText credit = (EditText)ll.findViewById(R.id.creditText);
                 String cr = credit.getText().toString();

                 datasource.createEntry(dr, "DR", amt);
                 datasource.createEntry(cr, "CR", amt);

                 Cursor c=datasource.database.rawQuery("SELECT * FROM entries", null);
                 c.moveToFirst();
              //   Log.i(LOGTAG, dr + " " + amt + " " + cr );// the last row

                 Log.i(LOGTAG, c.getString(0) + " " + c.getString(1) + " " + c.getString(2) );// shows always the first row
                 datasource.close();

                 startActivity(new Intent(EntryTab.this, Journals.class)); // error: the constructor intent is undefined.

        }
    });
    return ll;      
}
}
4

1 回答 1

0

一个活动可以有很多片段。在片段中,您可以通过getActivity()方法获取活动。

此外,要从活动 A -> 活动 B,我们使用意图。同样,要从 Fragment A -> Fragment B,在 Activity A 中,我们使用 FragmentTransactions。

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment f = new CustomFragment();
fragmentTransaction.add(R.id.fragment_container, f);
fragmentTransaction.commit();

另外,使用addToBackStack()方法,以便在回压时从片段 B 返回到片段 A。

于 2013-04-19T12:16:11.943 回答