单击片段android中的按钮打开新活动

时间:2014-06-02 07:44:33

标签: android android-fragments

我使用滑动操作选项卡我有3个选项卡(选项卡1,选项卡2,选项卡3)

如何通过单击片段android

中的按钮打开新活动

在标签页1中有多个按钮我要点击,例如按钮1打开新页面我该怎么办?

如何从片段中打开新页面?

<?xml version="1.0" encoding="utf-8"?>    
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"   
    android:layout_height="match_parent"    
    android:orientation="vertical"    
    android:background="#2e2e2e" >

   <Button    
        android:id="@+id/button1"    
        android:layout_width="match_parent"    
        android:layout_height="wrap_content"    
        android:layout_alignParentLeft="true"    
        android:layout_alignParentTop="true"    
        android:layout_marginTop="15dp"    
        android:background="#00b3ff"    
        android:text="button 1"    
        android:textColor="#ffffff"    
        android:textColorHint="#ffffff"    
        android:textSize="22sp"    
        android:textStyle="bold" />

    <Button
        android:id="@+id/button2"    
        android:layout_width="match_parent"    
        android:layout_height="wrap_content"    
        android:layout_alignLeft="@+id/button1"    
        android:layout_below="@+id/button1"    
        android:layout_marginTop="15dp"    
        android:background="#00b3ff"    
        android:text="button 2"     
        android:textColor="#ffffff"    
        android:textColorHint="#ffffff"    
        android:textSize="22sp"    
        android:textStyle="bold"/>       

</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

你可以这样做:

yourButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        Intent intent = new Intent(getActivity(),NextActivity.class);
        getActivity().startActivity(intent);
    }
});

答案 1 :(得分:0)

你只需要做你的意图。

this.getActivity.startActivity(this.getApplicationContext(), ClassName.class);

答案 2 :(得分:0)

    @SuppressLint("NewApi")
    public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "About", "Tips", "QuizGame" };

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

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

答案 3 :(得分:0)

公共类TipsFragment扩展了Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_quiz_game2, container, false);

    return rootView;
}

}