工具栏中的自定义按钮不响应操作。未显示吐司

Custom Buttons in ToolBar not responding to actions. Toast is not shown

我有一个非常基本的应用程序,我想在工具栏中添加一个复选框。

这就是我的 xml 的样子 :

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="xxxxxxxx.xx.xx.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:padding="5dp"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
            <EditText
                android:id="@+id/searchET"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="8"
                android:background="@android:color/white"/>

            <CheckBox
                android:id="@+id/favorite_toggle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                />
            </LinearLayout>
        </android.support.v7.widget.Toolbar>


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

    <include layout="@layout/content_home" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

在我的 MainActivity 中,我设置了一个简单的侦听器:

 favoriteBtn = (CheckBox) findViewById(R.id.favorite_toggle);

 favoriteBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(HomeActivity.this, "changed", Toast.LENGTH_SHORT);
                    addFavorite(url);
                }else{
                    Toast.makeText(HomeActivity.this, "again changed", Toast.LENGTH_SHORT);
                    removeFavorite(url);
                }
            }
        });

当我单击复选框时,没有任何反应,复选框切换但我没有看到 Toast。我在 logcat

中看到以下消息

D/ViewRootImpl: ViewPostImeInputStage processPointer 1

谁能告诉我哪里出了问题?

更新:我认为只是敬酒没有出现。

您需要在 Toast 之后添加 .show() 方法。所以它会像:

Toast.makeText(HomeActivity.this, "changed", Toast.LENGTH_SHORT).show();

如果没有该方法,您只是创建了 Toast 消息,但没有告诉系统显示它。

要显示 toast,您必须使用方法 show。所以在你的代码中:

Toast.makeText(HomeActivity.this, "changed", Toast.LENGTH_SHORT).show();