Android自定义工具栏不可点击

问题描述 投票:0回答:2

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

这是我的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);

    }
}

感谢您的帮助!

android user-interface android-xml
2个回答
0
投票

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

将此添加到您的取消中

android:onClick="cancel" 

并且比代码中的:

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

0
投票

我相信您会在调用按钮的活动中使用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
    }
}

另见:

© www.soinside.com 2019 - 2024. All rights reserved.