Android自定义工具栏不可点击

我创建了一个自定义工具栏,该工具栏的左侧有一个“取消”按钮,而写入时有一个“发布”按钮,这是大多数社交媒体应用程序使用的标准格式。

这是我的xml代码的一部分:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    style="@style/BarTheme"
    app:theme="@style/BarTheme"
    app:popuptheme="@style/Apptheme"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@color/white"
    app:title="Lend"
    android:elevation="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/add_item_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left|center_vertical"
            android:textSize="15dp"
            android:fontFamily="sans-serif-condensed-light"
            android:background="@null"
            android:textColor="@color/grey"
            android:text="CANCEL"></Button>

        <TextView
            android:id="@+id/add_item_post"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fontFamily="sans-serif-condensed-light"
            android:layout_gravity="bottom"
            android:textStyle="bold"
            android:gravity="center|center_vertical"
            android:textSize="30dp"
            android:text="NEW ITEM"></TextView>


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right|center_vertical"
            android:layout_weight="1"
            android:background="@null"
            android:fontFamily="sans-serif-condensed-light"
            android:textSize="15dp"
            android:layout_marginRight="16dp"
            android:textColor="@color/themeBlue"
            android:textStyle="bold"
            android:text="POST"></Button>
    </LinearLayout>
</androidx.appcompat.widget.Toolbar>

这是我的style.xml代码:     

<!-- Base application theme. -->
<style name="Apptheme" parent="Theme.AppCompat.Light.NoactionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/white</item>
    <item name="colorPrimaryDark">@color/white</item>
    <item name="coloraccent">@color/themeBlue</item>
</style>

<style name="BarTheme" parent="Theme.AppCompat.Light.DarkactionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/white</item>
    <item name="colorPrimaryDark">@color/white</item>
    <item name="coloraccent">@color/themeBlue</item>
</style>

</resources>

现在,在活动中,我决定至少尝试使用“取消”按钮,但此按钮不起作用。我尝试调试,但是它甚至都没有进入功能,因此我认为它甚至不可单击。

@Override
public void onClick(View view) {
    switch(view.getId()) {
        case R.id.add_item_cancel:
            Intent goBackintent = new Intent(AddItemactivity.this,Listingsactivity.class);
            startactivity(goBackintent);

    }
}

提前感谢您的帮助!

a1234567891989719 回答:Android自定义工具栏不可点击

如果您想覆盖onClick,Android会说,没有相应的onClick方法可以覆盖。这只是意味着,没有全局onClick,因此没有要覆盖的内容。

将此添加到您的取消

android:onClick="cancel" 

,而不是代码中的

public void cancel(View v){
//test if works        
Log.d(TAG,"cancel: ");
}
,

我相信您会在调用按钮的活动中使用onClickListener实现,但是您需要注册一个单击按钮时要调用的回调。

将以下代码行添加到您按钮所在的活动中的onCreate函数中:

protected void onCreate(Bundle savedValues) {
    ...
    Button button = (Button)findViewById(R.id.add_item_cancel);
    button.setOnClickListener(this);
}

实施OnClickListener回调:

public void onClick(View v) {
    switch(view.getId()) {
        case R.id.add_item_cancel:
            // do something when the button is clicked
    }
}

另请参见:

,

请确保您在onclick的“取消”按钮上注册了onCreate侦听器,或者在初始化取消按钮的位置注册了该监听器。

像下面这样:

addItemCancel.setOnClickListener(this);

接下来实现View.OnClickListener界面,如下所示:

public class SettingsActivity extends BaseActivity implements View.OnClickListener{

.............

}

然后重写如下所示的onclick方法:

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_item_cancel:
                onBackPressed();
                break;
        }
    }

如果仅在一个按钮或视图上使用了点击侦听器,则可以使用以下代码进行点击:

addItemCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

为此,您无需实现View.OnClickListener接口。

希望对您有帮助。

本文链接:https://www.f2er.com/3169320.html

大家都在问