如何覆盖按钮显示主页

时间:2018-10-25 10:36:51

标签: java android

默认情况下,按钮(setDisplayHomeAsUpEnabled)返回父活动,我该如何更改?例如:我需要回到Intent

    toolbar = (Toolbar) findViewById(R.id.tb_dados);
    toolbar.setTitle("Adicione uma descrição");
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

3 个答案:

答案 0 :(得分:3)

您可以尝试这个。...

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
        case android.R.id.home:

        // your intent code here.

                break;
        }

        return super.onOptionsItemSelected(item);
    }

答案 1 :(得分:1)

您可以使用onOptionsItemSelected方法并根据需要执行其他功能

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {
        Toast.makeText(this, "back Button Clicked", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

答案 2 :(得分:1)

由于您使用的是自定义工具栏,因此可以轻松使用这样的自定义工具栏

toolbar.setNavigationIcon(R.drawable.back_arrow);// use your back arrow image
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(this,MainActivity.class));//call which activity you want in back press
            finish();
        }
    });