Android应用程序在单击返回箭头按钮时没有响应。

huangapple go评论77阅读模式
英文:

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

问题

Hello Fellow developers!
我已经开发了一个使用Flickr API的Android应用程序,可以在用户请求时显示任何图像。我现在处于开发的最后阶段。一切看起来都很好,除了当您单击任何图像以展开详细视图并单击返回箭头返回时,没有响应...
任何帮助将不胜感激 Android应用程序在单击返回箭头按钮时没有响应。

ViewPhotoDetails.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("标题: " + photo.getTitle());

    TextView photoTags = (TextView) findViewById(R.id.photo_tags);
    photoTags.setText("标签: " + 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上的源代码

应用程序UI

英文:

Hello Fellow developers!
I have developed an Android App that uses the Flickr API to display any image upon users request. I am the final step of the development. Everything is looking great, except for when you click on any image to expand it in detail view & when you click the return arrow to go back - DOES NOT respond...
Any help would be greatly appreciated:)

ViewPhotoDetails.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(&quot;Title: &quot; + photo.getTitle());
TextView photoTags = (TextView) findViewById(R.id.photo_tags);
photoTags.setText(&quot;Tags: &quot; + 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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;ScrollView
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:flickr=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;
tools:context=&quot;com.example.flickrbrowser.ViewPhotoDetailsActivity&quot;
tools:showIn=&quot;@layout/photo_details&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;include
android:id=&quot;@+id/app_bar&quot;
layout=&quot;@layout/toolbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;?actionBarSize&quot; /&gt;
&lt;androidx.cardview.widget.CardView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;16dp&quot;
android:layout_marginRight=&quot;16dp&quot;
android:layout_marginTop=&quot;16dp&quot;
flickr:cardBackgroundColor=&quot;?colorPrimary&quot;
flickr:cardCornerRadius=&quot;8dp&quot;
flickr:cardPreventCornerOverlap=&quot;false&quot;
flickr:contentPaddingTop=&quot;16dp&quot;
flickr:contentPaddingBottom=&quot;16dp&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;FrameLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/photo_image&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginBottom=&quot;8dp&quot;
android:layout_marginLeft=&quot;8dp&quot;
android:layout_marginRight=&quot;8dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:scaleType=&quot;centerCrop&quot;
android:src=&quot;@drawable/placeholder&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/photo_author&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;top|center&quot;
android:paddingTop=&quot;8dp&quot;
android:textColor=&quot;@color/flickrSecondaryTextColor&quot;
android:textSize=&quot;14sp&quot; /&gt;
&lt;/FrameLayout&gt;
&lt;TextView
android:id=&quot;@+id/photo_title&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;8dp&quot;
android:layout_marginRight=&quot;8dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:textColor=&quot;@color/flickrPrimaryTextColor&quot;
android:textSize=&quot;14sp&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/photo_tags&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginLeft=&quot;8dp&quot;
android:layout_marginRight=&quot;8dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:textColor=&quot;@color/flickrPrimaryTextColor&quot;
android:textSize=&quot;10sp&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;/LinearLayout&gt;
&lt;/ScrollView&gt;

Source Code on GitHub

App UI

答案1

得分: 0

返回的翻译部分如下:

Back按钮的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 - 处理“设置”点击
    }

    return super.onOptionsItemSelected(item)
}

而不仅仅返回true/false,这样主页按钮将由super方法处理。

英文:

Back button has id: android.R.id.home. So you can handle it as

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

or just modify your

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item.itemId == R.id.action_settings) {
// TODO - handle &#39;settings&#39; click
}
return super.onOptionsItemSelected(item)
}

instead of returning just true/false so home button will be handled by super method.

答案2

得分: 0

正如@Boken所说,返回按钮(也称为“上”按钮)是一个具有ID android.R.id.home 的菜单按钮,因此当点击该按钮时,您可以像这样处理点击事件:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // 处理选项选择
    switch (item.getItemId()) { 
        // 您还可以使用if-else语句而不是switch语句(switch是更好的选择)
        case R.id.action_settings:
            return true;
        case android.R.id.home: // 当单击返回按钮时
            // 在这里处理此操作
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

如果您想返回到上一个屏幕,可以像这样使用 onBackPressed()

case android.R.id.home: // 当单击返回按钮时
    onBackPressed();
    return true;
英文:

Just as @Boken said, the back (also called the "up") button is a menu button which has id android.R.id.home so when that button is clicked you can handle the click like so:

@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);
}
}

if you want to return to the previous screen you use onBackPressed() like so:

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

答案3

得分: 0

以下是翻译好的部分:

"(更直接的答案)没有检查那个菜单项。必须更改ViewPhotoDetailsActivity.java中的onOptionsItemSelected方法:

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;
}
英文:

(More Direct Answer) Was not checking for that menu item. Had to change onOptionsItemSelected (in ViewPhotoDetailsActivity.java) to

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;
}

huangapple
  • 本文由 发表于 2020年8月5日 07:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63256422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定