【问题标题】:How can I save and restore the last Image View seen by the user?如何保存和恢复用户看到的最后一个图像视图?
【发布时间】:2021-02-05 06:52:13
【问题描述】:

我目前正在 Android Studio 中开发一款安卓音乐阅读应用。我使用的语言是 Java,但我是初学者(我的英语也很抱歉)。

该应用程序有几个练习。理想情况下,人们可以单击下一个或后退按钮来查看一个或另一个练习。问题是我也在寻找用户,即使他退出了应用程序,以继续看到上一个练习(图像)。

我已阅读有关活动循环的内容并且理解它,但由于我是该语言的初学者,我无法做我需要做的事情。我了解到 OnSaveInstanceState 或 OnRestoreInstanceState 不能解决我的问题,解决方案可能是使用 SharedPreferences 但我不知道如何将其应用于我目前拥有的代码。使用 SharedPreferences 我希望我可以保存和恢复上次看到的图像视图,并且我可以通过单击下一步或返回从那里继续。

这是当前代码,它允许我:显示初始练习并从一个练习转到另一个练习。

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private int image_index = 0 ;
    private static final int MAX_IMAGE_COUNT = 3;

    private int[] mImageIds = {
            R.raw.image1,
            R.raw.image2,
            R.raw.image3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showImage();
    }
    private void showImage() {
        ImageView imgView = (ImageView) findViewById(R.id.myimage);
        imgView.setImageResource(mImageIds[image_index]);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case (R.id.previous_btn):
                image_index--;
                if (image_index == -1) {
                    image_index = MAX_IMAGE_COUNT - 1;
                } else {
                }
                showImage();
                break;

            case (R.id.next_btn):
                image_index++;
                if (image_index == MAX_IMAGE_COUNT) {
                    image_index = 0;
                } else {
                }
                showImage();
                break;
        }
    }
}

这是xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="@android:color/background_light"
    tools:context=".MainActivity">

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

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:fontFamily="@font/gilroyextrabold"
                android:text="Ejercicios"
                android:textColor="@android:color/black"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/myimage"
                android:layout_width="wrap_content"
                android:layout_height="370dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView" />

            <LinearLayout
                android:id="@+id/linearLayout5"
                android:layout_width="match_parent"
                android:layout_height="54dp"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="40dp"
                android:layout_marginEnd="10dp"
                android:layout_marginRight="10dp"
                android:orientation="horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/myimage">

                <Button
                    android:id="@+id/previous_btn"
                    android:layout_width="190dip"
                    android:layout_height="wrap_content"
                    android:background="@drawable/btnatsslf"
                    android:onClick="onClick" />

                <Button
                    android:id="@+id/next_btn"
                    android:layout_width="190dip"
                    android:layout_height="wrap_content"
                    android:background="@drawable/btnsgtslf"
                    android:onClick="onClick" />
            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>
</ScrollView>

由于我没有那么多知识,我寻求你的帮助,我也得到了替代方案。

【问题讨论】:

    标签: java android sharedpreferences android-imageview


    【解决方案1】:

    将这些行放在setContentView 之后,showImage 之前 onCreate

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        image_index = sp.getInt("image_index", image_index);
    

    并将这个值保存在showImage 中,就在setImageResource 行之后

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        sp.edit().putInt("image_index", image_index).apply();
    

    你可以保留SharedPreferences sp作为类成员的引用,所以每次调用showImage时都不需要获取(也应该以同样的方式存储ImageView,而不需要每次都需要findViewById) .密钥 ("image_index") 也可以存储为一些 private static final String 类成员

    还请记住以备将来使用:这是您声明为静态的某个数组中的索引。当你改变它时,例如存储的值为 2 并且您剪切了 images 数组中的最后一项,然后用户在应用更新安装后获取索引越界异常,因此在 onCreate 内部检查该值是否在数组范围内(仅出于安全考虑)

    【讨论】:

    • 我不敢相信,它有效。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多