简体   繁体   中英

how to change to a new activity from a button click inside a fragmenttab

Hi i'm working on a app that has swipe tabs inside the tabs there is buttons so for example fragmenttab1 has Algabra button i want it when the user clicks this button it takes them to AlgabraHome.class i have no idea how to do this any help would be amazing!

fragmanttab1.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/algabra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="Algabra" />

    <Button
        android:id="@+id/geomtery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/algabra"
        android:layout_below="@+id/algabra"
        android:layout_marginTop="75dp"
        android:text="geomtery" />

</RelativeLayout>

FragmentTab1.java

package com.androidbegin.absviewpagertutorial;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.actionbarsherlock.app.SherlockFragment;



public class FragmentTab1 extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from fragmenttab1.xml
        View view = inflater.inflate(R.layout.fragmenttab1, container, false);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        setUserVisibleHint(true);
    }

}

if you need anything else please let me know

i'm new to android development and am learning so no non helpful comments please :)

Thanks

This easy ! Try this !

package com.androidbegin.absviewpagertutorial;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;

import com.actionbarsherlock.app.SherlockFragment;



public class FragmentTab1 extends SherlockFragment {
    @Override

    private Button btn;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from fragmenttab1.xml
        View view = inflater.inflate(R.layout.fragmenttab1, container, false);
        btn = view.findViewById(R.id.algabra);

        btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(FragmentTab1.this, AlgabraHome.class);
            startActivity(intent);
        }


        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        setUserVisibleHint(true);
    }

}

Don't forget to add to your AndroidManifest.xml file the activity :

   <activity
        android:name="com.androidbegin.AlgabraHome">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

I'm not sure if I forgot anything, I wrote this on the fly without testing it out cause I don't have the rest of your code.

Enjoy ! Let me know if it work

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