delete在工具栏上不工作的问题显示在那里

nqwrtyyt  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(350)

删除按钮 menu 代码

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/delete_menu_for_all"
        android:title="@string/delete"
        android:icon="@drawable/ic_delete"
        app:showAsAction="always"
        android:actionViewClass="android.widget.ImageView"
        tools:ignore="AppCompatResource" />
</menu>


Activity xml 代码

<androidx.appcompat.widget.Toolbar
        android:id="@+id/add_batch_toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        app:menu="@menu/delete_menu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


Activity Java 代码

Toolbar toolbar;
ImageView deleteButton;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_batch);
        findViews();
        setToolbar();
}
 private void setToolbar() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        toolbar.setTitleTextColor(getResources().getColor(R.color.black));
    }
   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.delete_menu,menu);
        deleteButton = (ImageView) menu.findItem(R.id.delete_menu_for_all).getActionView();
        if (deleteButton!=null){
            deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (batchId != 0){
                        Mbatch batch = db.batchDao().getBatchById(batchId);
                        db.batchDao().deleteBatch(batch);
                        startActivity(new Intent(AddBatch.this,BatchList.class));
                        finish();
                    }else {
                        Toast.makeText(AddBatch.this,"Course is not Saved Yet!!!",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
        return super.onCreateOptionsMenu(menu);
    }

我试着当巴顿。这是一个按钮。但在按钮删除图标没有显示正确,所以我想显示为图像。图像不是必需的。必须清楚地显示删除图标。我不知道怎么做。

tnkciper

tnkciper1#

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.delete_menu_for_all:
                if (batchId != 0){
                    Mbatch batch = db.batchDao().getBatchById(batchId);
                    db.batchDao().deleteBatch(batch);
                    startActivity(new Intent(AddBatch.this,BatchList.class));
                    finish();
                }else {
                    Toast.makeText(AddBatch.this,"Course is not Saved Yet!!!",Toast.LENGTH_SHORT).show();
                }
                break;

        }
        return true;
    }

我改变了覆盖方法

相关问题