Android 如何在当前选项卡布局中设置单击事件

Android 如何在当前选项卡布局中设置单击事件,android,onclick,android-tabhost,Android,Onclick,Android Tabhost,我有两个标签一个是登录,第二个是查询。我在两个选项卡中都设置了布局。但我不知道如何在登录活动和查询活动上设置单击事件。 举个例子,我现在在登录标签上。在登录我有两个按钮提交和取消,如果用户按下提交转到下一页..我怎么做呢 LoginEnquiryTab.java public class LoginEnquiryTab extends AppCompatActivity { TabHost myTabHost; //LocalActivityManager mlam;

我有两个标签一个是登录,第二个是查询。我在两个选项卡中都设置了布局。但我不知道如何在登录活动和查询活动上设置单击事件。 举个例子,我现在在登录标签上。在登录我有两个按钮提交和取消,如果用户按下提交转到下一页..我怎么做呢

LoginEnquiryTab.java

public class LoginEnquiryTab extends AppCompatActivity {

    TabHost myTabHost;
    //LocalActivityManager mlam;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler))
        {
            Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
        }
        //mlam = new LocalActivityManager(this, false);
        //mlam.dispatchCreate(savedInstanceState);
        myTabHost =(TabHost) findViewById(android.R.id.tabhost);
        myTabHost.setup();

        TabHost.TabSpec login = myTabHost.newTabSpec("tab1");
        TabHost.TabSpec enquiry = myTabHost.newTabSpec("tab2");


        //Below name is display on screen
        login.setIndicator(getResources().getString(R.string.btn_login));
        login.setContent(R.id.tab1);

        enquiry.setIndicator(getResources().getString(R.string.enquiry));
        enquiry.setContent(R.id.tab2);

        myTabHost.addTab(login);
        myTabHost.addTab(enquiry);

        myTabHost.setCurrentTab(0);
    }


    @Override
    protected void onResume(){
        super.onResume();
        //mlam.dispatchResume();
    }
    @Override
    protected void onPause(){
        super.onPause();
       // mlam.dispatchPause(isFinishing());
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">
        <LinearLayout android:id="@+id/tab1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <include layout="@layout/login" />
        </LinearLayout>
            <LinearLayout android:id="@+id/tab2"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <include layout="@layout/changepwd" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>
    </LinearLayout>

看,我制作了一个示例程序。但我使用片段作为选项卡内容-

ClassTabActivity.java

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TabHost;
import android.widget.Toast;

/**
 * Created by jimitpatel on 14/04/16.
 */
public class TabActivity extends AppCompatActivity implements OnTabEvent {

    private FragmentTabHost tabHost;

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

        // create the TabHost that will contain the Tabs
        tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
        TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
        TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");

        // Set the Tab name and Activity
        // that will be opened when particular Tab will be selected
        tab1.setIndicator("Tab1");
        tab2.setIndicator("Tab2");
        tab3.setIndicator("Tab3");

        /** Add the tabs  to the TabHost to display. */
        tabHost.addTab(tab1, Tab1Fragment.class, null);
        tabHost.addTab(tab2, Tab2Fragment.class, null);
        tabHost.addTab(tab3, Tab3Fragment.class, null);
    }

    @Override
    public void onButtonClick(String text) {
        if (!TextUtils.isEmpty(text) && null != tabHost) {
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();

            switch (text) {
                case "Tab1" :
                    tabHost.setCurrentTab(1);
                    break;
                case "Tab2" :
                    tabHost.setCurrentTab(2);
                    break;
                case "Tab3" :
                    tabHost.setCurrentTab(0);
                    break;
            }
        }
    }
}
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Tab1Fragment extends Fragment {

    private OnTabEvent mListener;

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);
        Button button = (Button) view.findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onButtonClick("Tab1");
            }
        });
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (OnTabEvent) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
        }
    }
}
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Tab2Fragment extends Fragment {

    private OnTabEvent mListener;

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab2, container, false);
        Button button = (Button) view.findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onButtonClick("Tab2");
            }
        });
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (OnTabEvent) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
        }
    }
}
public interface OnTabEvent {
    void onButtonClick(String text);
}
它是xml文件
activity\u tab.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"/>

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

</android.support.v4.app.FragmentTabHost>
ClassTab2Fragment.java

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TabHost;
import android.widget.Toast;

/**
 * Created by jimitpatel on 14/04/16.
 */
public class TabActivity extends AppCompatActivity implements OnTabEvent {

    private FragmentTabHost tabHost;

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

        // create the TabHost that will contain the Tabs
        tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
        TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
        TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");

        // Set the Tab name and Activity
        // that will be opened when particular Tab will be selected
        tab1.setIndicator("Tab1");
        tab2.setIndicator("Tab2");
        tab3.setIndicator("Tab3");

        /** Add the tabs  to the TabHost to display. */
        tabHost.addTab(tab1, Tab1Fragment.class, null);
        tabHost.addTab(tab2, Tab2Fragment.class, null);
        tabHost.addTab(tab3, Tab3Fragment.class, null);
    }

    @Override
    public void onButtonClick(String text) {
        if (!TextUtils.isEmpty(text) && null != tabHost) {
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();

            switch (text) {
                case "Tab1" :
                    tabHost.setCurrentTab(1);
                    break;
                case "Tab2" :
                    tabHost.setCurrentTab(2);
                    break;
                case "Tab3" :
                    tabHost.setCurrentTab(0);
                    break;
            }
        }
    }
}
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Tab1Fragment extends Fragment {

    private OnTabEvent mListener;

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);
        Button button = (Button) view.findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onButtonClick("Tab1");
            }
        });
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (OnTabEvent) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
        }
    }
}
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Tab2Fragment extends Fragment {

    private OnTabEvent mListener;

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab2, container, false);
        Button button = (Button) view.findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onButtonClick("Tab2");
            }
        });
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (OnTabEvent) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
        }
    }
}
public interface OnTabEvent {
    void onButtonClick(String text);
}
对于
Tab3Framgent
,也是如此。 这里的
TabOnEvent
是在
TabActivity
中实现的接口,它的回调用于片段类

接口TabOnEvent.java

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TabHost;
import android.widget.Toast;

/**
 * Created by jimitpatel on 14/04/16.
 */
public class TabActivity extends AppCompatActivity implements OnTabEvent {

    private FragmentTabHost tabHost;

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

        // create the TabHost that will contain the Tabs
        tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
        TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
        TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");

        // Set the Tab name and Activity
        // that will be opened when particular Tab will be selected
        tab1.setIndicator("Tab1");
        tab2.setIndicator("Tab2");
        tab3.setIndicator("Tab3");

        /** Add the tabs  to the TabHost to display. */
        tabHost.addTab(tab1, Tab1Fragment.class, null);
        tabHost.addTab(tab2, Tab2Fragment.class, null);
        tabHost.addTab(tab3, Tab3Fragment.class, null);
    }

    @Override
    public void onButtonClick(String text) {
        if (!TextUtils.isEmpty(text) && null != tabHost) {
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();

            switch (text) {
                case "Tab1" :
                    tabHost.setCurrentTab(1);
                    break;
                case "Tab2" :
                    tabHost.setCurrentTab(2);
                    break;
                case "Tab3" :
                    tabHost.setCurrentTab(0);
                    break;
            }
        }
    }
}
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Tab1Fragment extends Fragment {

    private OnTabEvent mListener;

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);
        Button button = (Button) view.findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onButtonClick("Tab1");
            }
        });
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (OnTabEvent) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
        }
    }
}
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Tab2Fragment extends Fragment {

    private OnTabEvent mListener;

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab2, container, false);
        Button button = (Button) view.findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onButtonClick("Tab2");
            }
        });
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (OnTabEvent) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
        }
    }
}
public interface OnTabEvent {
    void onButtonClick(String text);
}

我想这足以满足你的需要
onbutton单击按钮时,将从
Fragment
调用click
方法。接口将确保它在活动类中被调用,然后您可以根据需要更改选项卡。

使用回调/接口来解决您的问题如果您有任何教程,请与我共享..thanx for ur rpl