Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用noActionBar创建后退按钮_Java_Android_Back Button - Fatal编程技术网

Java 使用noActionBar创建后退按钮

Java 使用noActionBar创建后退按钮,java,android,back-button,Java,Android,Back Button,我已经开始学习Android开发了几天,我承认我的代码并不漂亮,因为我还有几周的时间学习Java。我试图在我的第二个主活动页面上创建一个后退按钮,以返回到第一个主活动,但我遇到的唯一解决方案是在使用工具栏时。在使用noActionBar时,如何添加此函数 主要活动xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res

我已经开始学习Android开发了几天,我承认我的代码并不漂亮,因为我还有几周的时间学习Java。我试图在我的第二个主活动页面上创建一个后退按钮,以返回到第一个主活动,但我遇到的唯一解决方案是在使用工具栏时。在使用noActionBar时,如何添加此函数

主要活动xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/clearwater"
android:scaleType="centerCrop"
tools:context="com.example.goodvibes.helloworld.MainActivity">



<Button
    android:id="@+id/button"
    style="@style/Widget.AppCompat.Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="146dp"
    android:background="@drawable/btn"
    android:fontFamily="cursive"
    android:text="DIVE"
    android:textSize="24sp"
    android:textStyle="bold" />




</RelativeLayout>
主要活动2 Java

package com.example.goodvibes.helloworld;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ViewAnimator;

public class activity_main_2 extends AppCompatActivity {




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




}
}
显示

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.goodvibes.helloworld">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" 
/>
        </intent-filter>
    </activity>
    <activity android:name=".activity_main_2"></activity>
</application>

</manifest>

首先,您必须添加一个必需的工具栏,在创建工具栏后,您将按钮放在工具栏内,如下所示

将xml布局文件更新为

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" >


        <ImageButton
            android:id="@+id/btn_back_toolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back"
            />

    </android.support.v7.widget.Toolbar>

将其添加到布局文件:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_acashmemoreport"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:theme="@style/ToolbarColoredBackArrow" />

呼叫完成;方法单击该按钮。简单:按照Rohit Singh的建议使用,可以使用imageview,并在imageview上单击可以调用finish;可能重复:-步骤1。将工具栏添加到布局中。并在活动中设置supportaction栏。第二步。然后通过调用getSupportActionBar.SetDisplayHomeAsupabledTrue启用后退按钮;第三步。因为后退按钮只是主页按钮,所以在OnOptions ItemSelected中处理其单击事件您反对覆盖onBackPressed吗?
public void ImageButton(View v)
{
    // some code
    finish();
}
<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" >


        <ImageButton
            android:id="@+id/btn_back_toolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back"
            />

    </android.support.v7.widget.Toolbar>
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageButton back=findViewById(R.id.btn_back_toolbar);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(this,AnotherActivity.class));
        }
    });
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_acashmemoreport"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:theme="@style/ToolbarColoredBackArrow" />
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_acashmemoreport);
setSupportActionBar(toolbar);


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}

// Show menu icon
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));