使用一个按钮即可切换标签 [英] using a button to switch between tabs

查看:158
本文介绍了使用一个按钮即可切换标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何创建一个按钮,这将有助于我的标签之间切换?

 按钮按钮1 =(按钮)findViewById(R.id.button1);
    button1.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){{
            //执行上的点击动作
                如果(v.getId()== R.id.button1){
                    的setContentView(R.layout.tab3);
                }
                否则如果(v.getId()== R.id.button1){
                    的setContentView(R.layout.main);
                }
            }
        }
    });
}}

这是我的标签1 code,这下code是在我tab.xml按钮。我想preSS下面的购买按钮,我想它去到另一个选项卡,这可能吗?

 <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_alignTop =@ + ID / imageButton1
        机器人:文字=买入
        安卓的onClick =TAB3/>

这是我的logcat文件

  16 01-05:04:20.903:D / dalvikvm(215):GC释放在96ms 806对象/ 70928字节
01-05 15:30:29.072:D / AndroidRuntime(213):关闭VM
01-05 15:30:29.082:W / dalvikvm(213):主题ID = 3:螺纹未捕获的异常退出(组= 0x4001b188)
01-05 15:30:29.082:E / AndroidRuntime(213):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
01-05 15:30:29.132:E / AndroidRuntime(213):java.lang.IllegalStateException:在活动中找不到的方法TAB3(视图)
01-05 15:30:29.132:E / AndroidRuntime(213):在android.view.View $ 1.onClick(View.java:2020)
01-05 15:30:29.132:E / AndroidRuntime(213):在android.view.View.performClick(View.java:2364)
01-05 15:30:29.132:E / AndroidRuntime(213):在android.view.View.onTouchEvent(View.java:4179)
01-05 15:30:29.132:E / AndroidRuntime(213):在android.widget.TextView.onTouchEvent(TextView.java:6541)
01-05 15:30:29.132:E / AndroidRuntime(213):在android.view.View.dispatchTouchEvent(View.java:3709)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693:E / AndroidRuntime(215):在com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
01-05 16:04:25.693:E / AndroidRuntime(215):在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.app.Activity.dispatchTouchEvent(Activity.java:2061)
01-05 16:04:25.693:E / AndroidRuntime(215):在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.os.Handler.dispatchMessage(Handler.java:99)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.os.Looper.loop(Looper.java:123)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.app.ActivityThread.main(ActivityThread.java:4363)
01-05 16:04:25.693:E / AndroidRuntime(215):在java.lang.reflect.Method.invokeNative(本机方法)
01-05 16:04:25.693:E / AndroidRuntime(215):在java.lang.reflect.Method.invoke(Method.java:521)
01-05 16:04:25.693:E / AndroidRuntime(215):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
01-05 16:04:25.693:E / AndroidRuntime(215):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-05 16:04:25.693:E / AndroidRuntime(215):在dalvik.system.NativeStart.main(本机方法)
01-05 16:04:25.693:E / AndroidRuntime(215):java.lang.NoSuchMethodException:产生的原因TAB3
01-05 16:04:25.693:E / AndroidRuntime(215):在java.lang.ClassCache.findMethodByName(ClassCache.java:308)
01-05 16:04:25.693:E / AndroidRuntime(215):在java.lang.Class.getMethod(Class.java:1014)
01-05 16:04:25.693:E / AndroidRuntime(215):在android.view.View $ 1.onClick(View.java:2017)
01-05 16:04:25.693:E / AndroidRuntime(215):25 ...更多
01-05 16:04:25.733的:I / dalvikvm(215):主题ID = 7:反应以信号3
01-05 16:04:25.733:I / dalvikvm(215):写堆栈跟踪/data/anr/traces.txt
01-05 16:04:27.505:I /流程(215):发送信号。 PID:215 SIG:9


解决方案

+1 Midoalageb和+1马克Gjol给你正是你需要切换选项卡的内容。他们离开了是code你正在尝试做的事件。你需要做的是建立在 TAB1 按钮,将有一个的onClick()监听器,将调用要么 getTabHost()setCurrentTabByTag(TAB2); getTabHost()setCurrentTab(1); 。无论是其中的一个将正常工作,只是取决于你想如何做到这一点。

你知道该怎么做的按钮和的onClick()听众?如果不是,谷歌有一些很好的例子。只是在你创建TabView的一个按钮,然后设置的onClick,然后把你的code在那里切换到您需要的标签。

<一个href=\"http://stackoverflow.com/questions/2541802/android-switch-tabs-from-within-an-activity-within-a-tab\">Another帖子这是非常相似,这一个可以帮助你。


getTabHost()setCurrentTabByTag(TAB2);

这会查找标有TAB2,并切换到该选项卡的标签。


getTabHost()setCurrentTab(1);

这将寻找标签,其中1指数变更为标签。


在问候你的XML

的XML文件主要用于定义抽象从你的意见实施的意见。这使您或其他开发人员在需要时提供给您的code不同的看法。它有助于提供一种方式来使用 MVC 模式。然而,这种模式是在Android中有一点不同,但XML文件是最常见认为在该模式中查看。

所以是,如果你将有多个按钮,他们将需要在XML中定义并在code语言实现的。


在问候你的按钮两难困境

从谷歌按钮的 API 的实际上为您提供了一个例子这个非常头版。请记住,Android是非常快的成熟作为开发平台,并在此了很多东西其实已经很好证明就行。 API文档也很不错,并给出很好的例子。通过那里去阅读它,你将最有可能解决很多更容易出现问题在那里。

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    TextView中的TextView =新的TextView(本);
    textview.setText(这是TAB1);
    的setContentView(TextView的);    最后Button按钮=(按钮)findViewById(R.id.button1);
    button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            //执行上的点击动作
            //此处进行切换到标签的事件,例如...
            getTabHost()setCurrentTabByTag(TAB2);
        }
    });
}

Does anyone know how to create a button which will help me to switch between tabs?

Button button1 = (Button) findViewById(R.id.button1);    
    button1.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {{
            // Perform action on click   
                if (v.getId() == R.id.button1) {
                    setContentView(R.layout.tab3);    
                }
                else if (v.getId() == R.id.button1) {
                    setContentView(R.layout.main);  
                }
            }
        }
    });
}}

this is the code in my tab 1, this next code is the button in my tab.xml. i want to press the buy button below and i want it to go to another tab, is this possible?

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageButton1"
        android:text="Buy"  
        android:onClick="tab3" />

this is my logcat file

01-05 16:04:20.903: D/dalvikvm(215): GC freed 806 objects / 70928 bytes in 96ms
01-05 15:30:29.072: D/AndroidRuntime(213): Shutting down VM
01-05 15:30:29.082: W/dalvikvm(213): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
01-05 15:30:29.082: E/AndroidRuntime(213): Uncaught handler: thread main exiting due to uncaught exception
01-05 15:30:29.132: E/AndroidRuntime(213): java.lang.IllegalStateException: Could not find a method tab3(View) in the activity
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.view.View$1.onClick(View.java:2020)
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.view.View.performClick(View.java:2364)
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.view.View.onTouchEvent(View.java:4179)
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
01-05 15:30:29.132: E/AndroidRuntime(213):  at android.view.View.dispatchTouchEvent(View.java:3709)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.os.Looper.loop(Looper.java:123)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.app.ActivityThread.main(ActivityThread.java:4363)
01-05 16:04:25.693: E/AndroidRuntime(215):  at java.lang.reflect.Method.invokeNative(Native Method)
01-05 16:04:25.693: E/AndroidRuntime(215):  at java.lang.reflect.Method.invoke(Method.java:521)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-05 16:04:25.693: E/AndroidRuntime(215):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-05 16:04:25.693: E/AndroidRuntime(215):  at dalvik.system.NativeStart.main(Native Method)
01-05 16:04:25.693: E/AndroidRuntime(215): Caused by: java.lang.NoSuchMethodException: Tab3
01-05 16:04:25.693: E/AndroidRuntime(215):  at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
01-05 16:04:25.693: E/AndroidRuntime(215):  at java.lang.Class.getMethod(Class.java:1014)
01-05 16:04:25.693: E/AndroidRuntime(215):  at android.view.View$1.onClick(View.java:2017)
01-05 16:04:25.693: E/AndroidRuntime(215):  ... 25 more
01-05 16:04:25.733: I/dalvikvm(215): threadid=7: reacting to signal 3
01-05 16:04:25.733: I/dalvikvm(215): Wrote stack trace to '/data/anr/traces.txt'
01-05 16:04:27.505: I/Process(215): Sending signal. PID: 215 SIG: 9

解决方案

+1 Midoalageb and +1 Mark Gjol have given you exactly what you need to switch the tab. What they are leaving out is the code for the event you are trying to do. What you need to do is set up a button on TAB1 that will have an onClick() listener that will call either getTabHost().setCurrentTabByTag(TAB2); or getTabHost().setCurrentTab(1);. Either one of those will work fine, just depends on how you want to do it.

Do you know how to do buttons and onClick() listeners? If not, google has some great examples. Just create a button in your tabView then set the onClick and then put your code in there for switching to the tab you want.

Another post that is very similar to this one that may help you out.


getTabHost().setCurrentTabByTag(TAB2);

This will look for the tab labeled "TAB2" and change to that tab.


getTabHost().setCurrentTab(1);

This will look for the tab with the index of "1" and change to that tab.


In regards to your xml

The xml files are mainly used to abstract defining your Views from the implementation of your Views. This allows you or other developers to provide different views to your code if needed. It helps provide a way to use the MVC pattern. However, this pattern is a little different in Android, but the xml file is most commonly considered the View in that pattern.

So Yes, if you are going to have multiple buttons they will need to be defined in your xml and implemented in your code.


In regards to your button dilema

The API for Button from google actually provides you with an example of this on the very front page. Keep in mind that Android is maturing very fast as a development platform and A LOT of this stuff is actually well documented already on line. The API documentation is also very good and gives great examples. Go by there and read it and you will most likely solve a lot of the more easy problems there.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView textview = new TextView(this);
    textview.setText("This is Tab1");
    setContentView(textview);

    final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            // perform the switch to tab event here for instance...
            getTabHost().setCurrentTabByTag("TAB2");
        }
    });
}

这篇关于使用一个按钮即可切换标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆