Java 如何在工具栏上的后退箭头上显示和设置单击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35810229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:09:30  来源:igfitidea点击:

How to display and set click event on Back Arrow on Toolbar?

javaandroidandroid-actionbarandroid-toolbarandroid-menu

提问by Abhijeet Mallick

Back button on toolbar

工具栏上的后退按钮

How can I set back arrow in Android toolbar and also apply click listener?

如何在 Android 工具栏中设置后退箭头并应用点击侦听器?

采纳答案by Pratik Tank

First make one toolbar.xml

先做一个 toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

then include it in activity_main.xmllike this way:

然后activity_main.xml像这样包含它:

<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

</LinearLayout>

then in your MainActivity.javafile, put this code:

然后在您的MainActivity.java文件中,输入以下代码:

mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("MyTitle");

To add listener on back press, use following method:

要在按下后添加侦听器,请使用以下方法:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // todo: goto back activity from here

            Intent intent = new Intent(CurrentActivity.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

回答by USER9561

Add this

添加这个

 Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
 setSupportActionBar(toolbar);
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 getSupportActionBar().setDisplayShowHomeEnabled(true);

and in onOptionsItemSelectedadd this

onOptionsItemSelected添加这个

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Check this

检查这个

Display Back Arrow on Toolbar Android

在 Android 工具栏上显示后退箭头

http://developer.android.com/intl/es/training/implementing-navigation/ancestral.html

http://developer.android.com/intl/es/training/implementing-navigation/ancestral.html

回答by Ahmad Aghazadeh

If you want to know when home is clicked is an AppCompatActivity then you should try it like this: Use this code :

如果您想知道单击 home 的时间是 AppCompatActivity,那么您应该像这样尝试:使用以下代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

Listen for click events on android.R.id.home like usual:

像往常一样监听 android.R.id.home 上的点击事件:

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    if (menuItem.getItemId() == android.R.id.home) {
         Intent intent = new Intent(CurrentActivity.this, MainActivity.class);
         startActivity(intent);
         finish();
    }
    return super.onOptionsItemSelected(menuItem);
}

回答by Shashank Udupa

If you are using the default back button for android by using

如果您使用 android 的默认后退按钮,请使用

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Then override the onOptionsItemSelected like

然后像覆盖 onOptionsItemSelected

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            //do whatever
            return true;
        default:
           return super.onOptionsItemSelected(item);
    }
}

回答by sagar.android

Toolbar mToolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);    
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // perform whatever you want on back arrow click 
    }
});

// with lamda expression

// 使用 lamda 表达式

toolbar.setNavigationOnClickListener(view -> onBackPressed());

回答by Code Spy

Complete example herehttp://www.freakyjolly.com/how-to-add-back-arrow-in-android-activity/

完整的例子在这里http://www.freakyjolly.com/how-to-add-back-arrow-in-android-activity/

Use getSupportActionBar()Activity on which you want to show Back Icon

使用要在其上显示后退图标的getSupportActionBar()Activity

In OtherActivity.class

OtherActivity.class 中

public class OtherActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.other_activity);


    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

public boolean onCreateOptionsMenu(Menu menu) {
    return true;
}

}

}

This will add a event listen

这将添加一个事件监听

回答by Asad Mehmood

Very simple code. Add inside onCreateView()method of activity

非常简单的代码。添加内部onCreateView()活动方法

To display icon

显示图标

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and to add click listener

并添加点击监听器

 toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Put your click logic here
            }
        });