android选项卡项onClick不';行不通

android选项卡项onClick不';行不通,android,onclick,tabitem,Android,Onclick,Tabitem,我有功能cityClick, 如果我从textView调用此函数,它工作正常,但如果我从TabItem调用cityClick,它不工作,发生了什么 爪哇 布局 <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"

我有功能cityClick, 如果我从textView调用此函数,它工作正常,但如果我从TabItem调用cityClick,它不工作,发生了什么

爪哇

布局

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:clickable="true"
        android:onClick="cityClick"
        app:tabMode="fixed">

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:textSize="18sp"
            android:onClick="cityClick"
            android:clickable="true"
            android:text="GDAŃSK" />

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:textSize="18sp"
            android:onClick="cityClick"
            android:clickable="true"
            android:text="SOPOT" />

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:textAlignment="center"
            android:textSize="18sp"
            android:onClick="cityClick"
            android:clickable="true"
            android:text="GDYNIA" />

    </android.support.design.widget.TabLayout>

我在这个论坛上搜索,但没有找到任何帮助。

试试这个

  tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Toast.makeText(mActivity, "hai", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
在科特林

tabs.addOnTabSelectedListener(object:TabLayout.OnTabSelectedListener {
    override fun onTabSelected(tab : TabLayout.Tab) {
        Toast.makeText(mActivity, tab.text, Toast.LENGTH_SHORT).show()
    }
    override fun onTabUnselected(p0: TabLayout.Tab?) {

    }
    override fun onTabReselected(p0: TabLayout.Tab?) {

    }
})

由于可能存在多个选项卡,这将更加完整:

TabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
//                        codes related to the first tab
                        break;
                    case 1:
//                        codes related to the second tab
                        break;
                    case 2:
//                        codes related to the third tab
                        break;
                    case 3:
//                        codes related to the fourth tab
                        break;
                        .
                        .
                        .
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

检查这是否有效。这里的解决方案从少到复杂,有没有一种简单的方法来调用一个小函数,而不需要几十行额外的代码?我想选民们应该给出一个解决方案……对我来说,这是一个符合SO准则的有效问题……没有理由否决投票。@slawek:您是否在textView中使用了完全相同的
cityClick
?是的,这与textView的功能和工作原理相同
TabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
//                        codes related to the first tab
                        break;
                    case 1:
//                        codes related to the second tab
                        break;
                    case 2:
//                        codes related to the third tab
                        break;
                    case 3:
//                        codes related to the fourth tab
                        break;
                        .
                        .
                        .
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });