简体   繁体   中英

Open new activity by clicking the button in fragment android

I use swipe action tab I have 3 tabs (tab 1,tab 2,tab 3)

How can I open new activity by clicking the button in fragment android

in tab 1 page there is multiple buttons I want to click for example button 1 to open new page how can I do that?

how can I open new page from fragment ?

<?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>

You can do this:

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

您只需要做您的意图。

this.getActivity.startActivity(this.getApplicationContext(), ClassName.class);
    @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) {
    }

}

public class TipsFragment extends Fragment {

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

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

    return rootView;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM