每日总结3.8

对菜单栏里添加更多按钮

首先新建一个menu布局文件

 

 

 

 调用以下函数实现删除

 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.delete:
                //new AlertDialog.Builder(EditActivity.class)
                new AlertDialog.Builder(EditActivity.this)
                        .setMessage("确认删除吗?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(openMode==4){
                            intent.putExtra("mode",-1);
                            setResult(RESULT_OK,intent);
                        }else {
                            intent.putExtra("mode",2);
                            intent.putExtra("id",id);
                            setResult(RESULT_OK,intent);
                        }
                        finish();
                    }
                }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create().show();

                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

posted @ 2023-03-09 19:58  cojames  阅读(11)  评论(0编辑  收藏  举报