android - 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);
}
}
提前感谢您的帮助!
解决方案
我相信您正在使用onClickListener
实现作为调用按钮的活动的一部分,但是您需要注册一个回调以在单击按钮时调用。
将以下代码行添加到onCreate
按钮所在的 Activity 中的函数中:
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
}
}
也可以看看:
推荐阅读
- ios - search tuples object in UIsearchbar?
- mysql - 如何在 sql 查询中获取价格差异百分比
- python - 如何创建 datetime.time 类的子类
- laravel - 如何修复未定义函数:7 错误:运算符不存在:整数 = 文本
- javascript - 如何将 Python+Flask 对象列表转换为 JS 对象列表?
- python - Python:尝试检查文件是否存在,如果不存在,则创建新文件和最终输出列表
- reactjs - 盖茨比:更改默认路径名称
- javascript - (let { items } = user) 是否与说 (user.items) 完全相同?
- c - 返回指向 const int 的 const 指针
- python - 如何在 Windows PC 上安装 pygalmesh(取决于 Eigen)?