Android 如何在不同的按钮点击时设置不同的片段

Android 如何在不同的按钮点击时设置不同的片段,android,android-fragments,android-button,Android,Android Fragments,Android Button,我正在使用夏洛克图书馆。我的问题是我无法更改不同按钮点击的片段 这是我的密码: public class AmecmainActivity extends FragmentActivity { private ImageButton legislativebutton; private ImageButton About; private ImageButton OutageReport; private ImageButton ViewPageReportLeft; private Imag

我正在使用夏洛克图书馆。我的问题是我无法更改不同按钮点击的片段

这是我的密码:

public class AmecmainActivity extends FragmentActivity {

private ImageButton legislativebutton;
private ImageButton About;
private ImageButton OutageReport;
private ImageButton ViewPageReportLeft;
private ImageView ViewPageReportRight;
private ImageButton actionnetwork;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.amecmain);

  actionnetwork=(ImageButton)findViewById(R.id.Actionnetwork);
  actionnetwork.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Bundle b = new Bundle();
        b.putString("action", "actionnetwork");

        Intent intent = new Intent(v.getContext(),
                FragmentChangeActivity.class);
        intent.putExtras(b);
        startActivity(intent);
    }
});



  ViewPageReportLeft.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Bundle b = new Bundle();
        b.putString("html", "htmlcontent");
        Intent intent = new Intent(v.getContext(),
                FragmentChangeActivity.class);
        intent.putExtras(b);
        startActivity(intent);  
    }
});    
}
在这里,我使用了一个通用的功能

public class FragmentChangeActivity extends SlidingFragmentActivity {

private Fragment mContent;

public FragmentChangeActivity() {
    super();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    Intent in = getIntent();
    Bundle b = in.getExtras(); 
    String name= b.getString("action");

           //Here i am getting the values through intent so that i can give that name 
            //  set the fragment.

    // set the Above View
    if (savedInstanceState != null)
        mContent = getSupportFragmentManager().getFragment(b, "action");


    if (mContent==name)// Here i am setting fragments i dnot know to check the  
                                 // condition here mContent is fragment and i am giving 
                                //  string how to check the condition.  

        mContent = new Fragment1(); //here i am setting fragment 


    // set the Above View
    setContentView(R.layout.content_frame);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, mContent).commit();

    // set the Behind View
    setBehindContentView(R.layout.menu_frame);
    getSupportFragmentManager().beginTransaction()
        .replace(R.id.menu_frame, new ColorMenuFragment()).commit();
    @Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getSupportFragmentManager().putFragment(outState, "mContent", mContent);
           }

public void switchContent(Fragment fragment) // Here is switchcontent method which 
                                                //   will change the fragment content
   {
    mContent = fragment;
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, fragment).commit();
    getSlidingMenu().showContent();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) 
    {
    case R.id.menuIcon:
         toggle();
         break; 
     case android.R.id.home:
      getFragmentManager().executePendingTransactions();
            }
    return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

任何一个回答这个问题的人,请检查片段是如何被调用的。请查看本教程: