简体   繁体   English

如何通过单击Android中的按钮从一个选项卡切换到另一个选项卡?

[英]How to switch from one tab to another tab by clicking on button in Android?

I trying to write a code in Android, to switch from one tab to another tab by click on a button. 我试图在Android中编写代码,通过单击一个按钮从一个选项卡切换到另一个选项卡。

I know to by clicking on tab we can switch from one tab to another but can it be possible to switch from one tab to another tab by clicking on one button. 我知道通过单击选项卡,我们可以从一个选项卡切换到另一个选项卡,但是可以通过单击一个按钮从一个选项卡切换到另一个选项卡。

I tried the following tutorial. 我尝试了以下教程。

http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

In this tutorial i have edited the MovieFragment.java file in following manner. 在本教程中,我以以下方式编辑了MovieFragment.java文件。

MovieFragment.java MovieFragment.java

package info.androidhive.tabsswipe;

import info.androidhive.tabsswipe.R;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MoviesFragment extends Fragment {

ViewPager viewPager;

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

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

    Button btn = (Button) rootView.findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {

            viewPager.setCurrentItem(0);

        }
    }); 


    return rootView;
}
}

XML Code XML代码

   <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="#17df0d">

<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Design Movies Screen"
    android:textSize="20dp"
    android:layout_centerInParent="true"/>

<Button 
    android:id="@+id/btn"
    android:layout_width="100dp"
    android:layout_height="80dp"
    android:text="Click"
    />

I have created a button in the xml layout and trying switch to tab -1 but i am getting Null Pointer Exception. 我在xml布局中创建了一个按钮,并尝试切换到选项卡-1,但我收到了空指针异常。

Error : 错误:

05-20 09:07:10.995: E/AndroidRuntime(1117): FATAL EXCEPTION: main
05-20 09:07:10.995: E/AndroidRuntime(1117): java.lang.NullPointerException
05-20 09:07:10.995: E/AndroidRuntime(1117):     at info.androidhive.tabsswipe.MoviesFragment$1.onClick(MoviesFragment.java:33)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.view.View.performClick(View.java:4240)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at  android.view.View$PerformClick.run(View.java:17721)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Handler.handleCallback(Handler.java:730)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Looper.loop(Looper.java:137)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.app.ActivityThread.main(ActivityThread.java:5103) 
05-20 09:07:10.995: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invokeNative(Native Method)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invoke(Method.java:525) 
05-20 09:07:10.995: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-20 09:07:10.995: E/AndroidRuntime(1117):     at dalvik.system.NativeStart.main(Native Method)

Please suggest me some solution or some good technique to do it. 请给我建议一些解决方案或一些好的技术。

Have a look at Creating Swipe Views with Tabs and at the documentation for ViewPager.setCurrentItem() : you can put a call to this method on the event listener of your Button. 看看使用Tabs创建滑动视图ViewPager.setCurrentItem()的文档:您可以在Button的事件侦听器上调用此方法。 Please share some code of yours to get a more targeted solution to your problem. 请分享您的一些代码,以获得针对性更强的解决方案。

You need to amend your code like this: 您需要像这样修改代码:

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

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

        btn = (Button) rootView.findViewById(R.id.btn);
        btnWeb.setOnClickListener(new OnClickListener() {

        @Override
            public void onClick(View v) {
               Intent newFrag = new Intent(getActivity(), NewFragment.class);
               startActivity(newFrag);
            });
        }
    });

    return rootView;

I would also suggest you use ViewPager to do that. 我还建议您使用ViewPager来做到这一点。 A great tutorial for this can be found here : 一个很好的教程可以在这里找到:

Hope this helps you :) 希望这对您有所帮助:)

    Button registerButton = (Button) rootView
            .findViewById(R.id.register_btn);
    registerButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            viewPager = (ViewPager) getActivity().findViewById(
                    R.id.viewpager);
            viewPager.setCurrentItem(1);

            // set tab postion 1  to move next tab 

        }

If you are in a fragment page(lets say first page) which has a button to switch to another fragment page(lets say at position x) in view pager then you can use : 如果您位于片段页面(例如说第一页)中,而该页面在视图分页器中有一个按钮可以切换到另一个片段页面(例如说在位置x上),则可以使用:

ViewPager viewPager = getParentFragment().getView().findViewById(R.id.viewpager_fragment);
        viewPager.setCurrentItem(position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过单击选项卡1中的按钮切换到选项卡2? - How to switch to tab 2 by clicking a button in tab 1? android无法从一个选项卡切换到另一个 - android Not able to switch from One tab to another 如何将参数从一个选项卡传递到另一个选项卡并使用按钮更改选项卡? - How to pass a parameter from one tab to another and change the tab with a button? 如何通过单击Android当前选项卡中的按钮来转到其他选项卡? - How to go other Tabs by clicking on a Button from the current Tab in Android? 如何在不单击Android Studio上的选项卡的情况下移至另一个选项卡? - How to move to another tab without clicking the tab on Android Studio? 通过按钮从一个选项卡视图移动到另一个视图单击Android ActionBar - Moving From one tab view to another by button Click Android ActionBar android-如何从另一个选项卡重新启动选项卡 - android - How to restart the tab from another tab Android | 如何显示从一个标签页滑动到另一个标签页的免责声明 - Android | How to show a disclaimer note on swiping from one tab to another 单击选项卡时从一个活动移动到另一个活动 - Moving from one activity to another while clicking tab Android:如何通过单击每个不同的按钮来设置当前选项卡? - Android : How to set a current tab selected by clicking on each different button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM