簡體   English   中英

Android 單擊后退箭頭按鈕時應用程序沒有響應

[英]Android App doesn't respond when clicking on the back-arrow button

各位開發者您好。 我開發了一個 Android 應用程序,它使用 Flickr API 來根據用戶請求顯示任何圖像。 我是開發的最后一步,一切看起來都很棒。 除非您單擊任何圖像以將其展開詳細視圖以及單擊返回箭頭以返回 go 時 - 不響應..:任何幫助將不勝感激:)

查看照片詳情。java:

package com.example.flickrbrowser;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import androidx.annotation.RequiresApi;

public class ViewPhotoDetailsActivity extends BaseActivity {

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photo_details);
    activateToolbarWithHomeEnabled();

    Intent intent = getIntent();
    Photo photo = (Photo) intent.getSerializableExtra(PHOTO_TRANSFER);

    TextView photoTitle = (TextView) findViewById(R.id.photo_title);
    photoTitle.setText("Title: " + photo.getTitle());

    TextView photoTags = (TextView) findViewById(R.id.photo_tags);
    photoTags.setText("Tags: " + photo.getTags());

    TextView photoAuthor = (TextView) findViewById(R.id.photo_author);
    photoAuthor.setText(photo.getAuthor());

    ImageView photoImage = (ImageView) findViewById(R.id.photo_image);
    Picasso.with(this).load(photo.getLink())
            .error(R.drawable.placeholder)
            .placeholder(R.drawable.placeholder)
            .into(photoImage);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_photo_details, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return false;
}
}    

photo_details.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flickr="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.flickrbrowser.ViewPhotoDetailsActivity"
tools:showIn="@layout/photo_details">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/app_bar"
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize" />

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        flickr:cardBackgroundColor="?colorPrimary"
        flickr:cardCornerRadius="8dp"
        flickr:cardPreventCornerOverlap="false"
        flickr:contentPaddingTop="16dp"
        flickr:contentPaddingBottom="16dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/photo_image"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/placeholder" />

                <TextView
                    android:id="@+id/photo_author"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top|center"
                    android:paddingTop="8dp"
                    android:textColor="@color/flickrSecondaryTextColor"
                    android:textSize="14sp" />

            </FrameLayout>

            <TextView
                android:id="@+id/photo_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:textColor="@color/flickrPrimaryTextColor"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/photo_tags"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:textColor="@color/flickrPrimaryTextColor"
                android:textSize="10sp" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>

GitHub上的源代碼

應用程序界面

后退按鈕的 id 為: android.R.id.home 所以你可以把它當作

if (item?.itemId == android.R.id.home) {
    finish()
}

或者只是修改你的

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    if (item.itemId == R.id.action_settings) {
        // TODO - handle 'settings' click
    }

    return super.onOptionsItemSelected(item)
}

而不是只返回true/false ,所以主頁按鈕將由super方法處理。

正如@Boken 所說,后退(也稱為“向上”)按鈕是一個菜單按鈕,其 ID android.R.id.home所以當點擊該按鈕時,您可以像這樣處理點擊:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) { 
    //You can also use if-else statements instead of switch (switch is a better option)
        case R.id.action_settings:
            return true;
        case android.R.id.home: //when the back button is clicked
            // handle this action here
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

如果你想返回上一個屏幕,你可以使用onBackPressed()像這樣:

    case android.R.id.home: //when the back button is clicked
        onBackPressed();
        return true;

(更直接的回答)沒有檢查那個菜單項。 必須將 onOptionsItemSelected(在 ViewPhotoDetailsActivity.java 中)更改為

public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
    return true;
} else if (id == android.R.id.home) {
    finish();
}
return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM