简体   繁体   English

Android:如何通过单击每个不同的按钮来设置当前选项卡?

[英]Android : How to set a current tab selected by clicking on each different button?

I have an app in which i have three buttons in the first page(For example say A, B, C buttons). 我有一个应用程序,其中我在第一页有三个按钮(例如说A,B,C按钮)。 When i click on any one of these three buttons they will navigate to same tabActivity page. 当我点击这三个按钮中的任何一个时,他们将导航到相同的tabActivity页面。 By default first tab is getting selected. 默认情况下,第一个选项卡被选中。 But what i want is, when i click on A button first tab should be get selected. 但我想要的是,当我点击A按钮时,第一个选项卡应该被选中。 when i click on B button second tab should be get selected. 当我点击B按钮时,应该选择第二个标签。 when i click on C button third tab should get selected. 当我点击C按钮时,第三个标签应该被选中。 I dont know how to do this. 我不知道该怎么做。 Can any one help me on this? 谁可以帮我这个事?

Thanks in advance... 提前致谢...

Try This, 尝试这个,

on clicking button A 点击按钮A.

tabHost.setCurrentTab(0); 

on clicking button B 点击按钮B

tabHost.setCurrentTab(1); 

on clicking button C 点击按钮C

tabHost.setCurrentTab(2);

in your activity class try this and in my app is working just fine. 在你的活动课中尝试这个,并在我的应用程序工作正常。

Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab 
        Intent intent;  // Reusable Intent for each tab

            // Create an Intent to launch an Activity for the tab (to be reused) 
           intent = new Intent().setClass(this, ArtistsActivity.class);
            // Initialize a TabSpec for each tab and add it to the TabHost
            spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                              res.getDrawable(R.drawable.ic_tab_artists))
                          .setContent(intent);
            tabHost.addTab(spec);

            // Do the same for the other tabs 
           intent = new Intent().setClass(this, AlbumsActivity.class); 
           spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
                             res.getDrawable(R.drawable.ic_tab_albums))
                          .setContent(intent);
            tabHost.addTab(spec);

            intent = new Intent().setClass(this, SongsActivity.class);
            spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                              res.getDrawable(R.drawable.ic_tab_songs)).setContent(intent);
            tabHost.addTab(spec);   
        }

Hope it helps you 希望它能帮到你

暂无
暂无

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

相关问题 如何通过单击Android当前选项卡中的按钮来转到其他选项卡? - How to go other Tabs by clicking on a Button from the current Tab in Android? 如何通过单击Android中的按钮从一个选项卡切换到另一个选项卡? - How to switch from one tab to another tab by clicking on button in Android? Kotlin (android studio) - 通过单击更改选项卡不会更改所选片段上的按钮,滑动到选项卡有效 - Kotlin (android studio) - changing tab by clicking doesn't change button on selected fragment, where swiping to the tab works 如何通过单击android中的按钮绘制不同的形状? - How to draw different shapes by clicking a button in android? 单击不同的按钮时如何在Fragment的Textview中设置不同的字符串? - How to set Different String in a Textview in Fragment while clicking different Button? 单击android项目中的按钮后,如何在地图上获取当前位置? - How to get a current location on map after clicking button in android project? 如何通过单击选项卡1中的按钮切换到选项卡2? - How to switch to tab 2 by clicking a button in tab 1? 在Android TabLayout中未获取当前选定的Tab位置 - Not getting current selected Tab position in android tablayout 如何在android中的Button Click事件的Current Tab中启动新的actvity - How to start new actvity in the Current Tab on Button Click event in android Android-如何将操作栏标签设置为首次选择? - Android - How to set an action bar tab as first selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM