SharedPreferences save a button change invisible after click on it and let appears another button

只愿长相守 提交于 2020-01-13 20:48:08

问题


I'm new here. I'm french so maybe my english is not very good, sorry for that.

I'm a beginner in Android development, I got to create an app for finish my study.

I have an activity number 1 called VoeuxActivity.java with 8 buttons (and 8 TextViews), they are all VISIBLEs at the beginning, when an user click on one of them button change by INVISIBLE (user can't see the button after clicked on it), when I quit the app and I come back again on my app, the button is always invisible thanks to the SharedPreferences and a member of this forum. But I want now when I clicked on this button named "totoB" it will be invisible and another button becomes Visible on another activity number 2 called PersoActivity.java because the first activity is for unlock some characters locked, when I choose a new character in activity 1, it will be invisible in activity 1 and will be visible on activity 2 in order to choose 2 characters for a fight (that's why there's a boolean name "isClicked") but I tried to use SharedPreferences for stay the button visible on the second activity but it doesn't work at all. When a new character is unlock if I quit the app and come back again in my app, the new character unlock isn't save as visible and he's Invisible again but I want he's visible always by the SharedPreferences. I post a court code with the same button on my first activity then on the second activity (I try to do the same as the first activity but my solution can't work), maybe u could help me to resolve my issue.

The code of the first activity can work and save the changements:

public class VoeuxActivity extends Activity {
Button totoB;
TextView totoTv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_voeux);

    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);

    totoB = (Button) findViewById(R.id.perso1);
    totoTv = (TextView) findViewById(R.id.perso1Text);
    totoB.setVisibility(prefs.getBoolean("isTotoBVisible", true) ? View.VISIBLE : View.INVISIBLE);
    totoTv.setVisibility(prefs.getBoolean("isTotoTVVisible", true) ? View.VISIBLE : View.INVISIBLE);

    totoB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            prefs.edit().putBoolean("isTotoBVisible", false).apply();
            prefs.edit().putBoolean("isTotoTVVisible", false).apply();

            totoB.setVisibility(View.INVISIBLE);
            totoTv.setVisibility(View.INVISIBLE);
            Intent intentToto = new Intent(VoeuxActivity.this, JouerActivity.class);
            startActivity(intentToto);
        }
    });
}

I try to do the same thing for the second activity but it can't work this time, the changement aren't saved.

public class PersoActivity extends Activity {

public static Personnage p1, p2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_perso);

    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);

    final Button totoPersoBtn = (Button) findViewById(R.id.perso1);
    final TextView totoPersoTv = (TextView) findViewById(R.id.perso1Text);
    totoAdversaireBtn = (Button) findViewById(R.id.adversaire1);
    totoAdversaireTv = (TextView) findViewById(R.id.adversaire1Text);
    totoPersoBtn.setVisibility(prefs.getBoolean("isTotoPersoBtnInvisible", true) ? View.INVISIBLE : View.VISIBLE);
    totoPersoTv.setVisibility(prefs.getBoolean("isTotoPersoTvInvisible", true) ? View.INVISIBLE : View.VISIBLE);

totoAdversaireBtn.setVisibility(prefs.getBoolean("isTotoAdversaireBtnInvisible", true) ? View.INVISIBLE : View.VISIBLE);

totoAdversaireTv.setVisibility(prefs.getBoolean("isTotoAdversaireTvInvisible", true) ? View.INVISIBLE : View.VISIBLE);

    if(VoeuxActivity.isClicked) {
        prefs.edit().putBoolean("isTotoPersoBtnInvisible", false).apply();
        prefs.edit().putBoolean("isTotoPersoTvInvisible", false).apply();
        prefs.edit().putBoolean("isTotoAdversaireBtnInvisible", false).apply();
        prefs.edit().putBoolean("isTotoAdversaireTvInvisible", false).apply();
        totoPersoTv.setVisibility(View.VISIBLE);
        totoPersoBtn.setVisibility(View.VISIBLE);
        totoAdversaireBtn.setVisibility(View.VISIBLE);
        totoAdversaireTv.setVisibility(View.VISIBLE);
    } else {
        totoPersoBtn.setVisibility(View.INVISIBLE);
        totoPersoTv.setVisibility(View.INVISIBLE);
        totoAdversaireBtn.setVisibility(View.INVISIBLE);
        totoAdversaireTv.setVisibility(View.INVISIBLE);
    }}}

How can I save the changement of the Button and the TextView from Visible to Invisible in the second activity? Thank u very much if someone can helps me because I really don't know why it's not work at all. Giggs


回答1:


I made an example app and tested it. It saves the last state of textviews(visible or invisible) no matter on which activity you make changes. And even when you exit and come back to the app it loads the last state.

Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.key.hs.invisiblebuttons">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".Activity1"
            android:label="Test1"
            android:alwaysRetainTaskState="true"
            android:configChanges="keyboardHidden|orientation|screenSize"
            >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"></action>
            <category android:name="android.intent.category.HOME"></category>
            <category android:name="android.intent.category.LAUNCHER"></category>

        </intent-filter>
        </activity>

        <activity
            android:name=".Activity2"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:hardwareAccelerated="true">
        </activity>
    </application>
</manifest>

Activity1.class

package com.key.hs.invisiblebuttons;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    /**
     * Created by Hasan on 26.04.2017.
     */

    public class  Activity1 extends AppCompatActivity {

        private boolean btn1visiblity, btn2visibility;
        private TextView tv1, tv2;
        private Button btn1, btn2, act2, reset;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity1lay);

            tv1= (TextView)findViewById(R.id.tv1);
            tv2=(TextView)findViewById(R.id.tv2);
            btn1= (Button)findViewById(R.id.btn1);
            btn2=(Button)findViewById(R.id.btn2);
            act2=(Button)findViewById(R.id.act2);
            reset=(Button)findViewById(R.id.reset);


            //Create or load preferences
            final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
            btn1visiblity = prefs.getBoolean("TV1visibility", true);
            btn2visibility = prefs.getBoolean("TV2visibility", true);

            //take into effect saved booleans
            if(btn1visiblity){
                tv1.setVisibility(View.VISIBLE);
            }else{
                tv1.setVisibility(View.INVISIBLE);
            }
            if(btn2visibility){
                tv2.setVisibility(View.VISIBLE);
            }else{
                tv2.setVisibility(View.INVISIBLE);
            }


            reset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                    btn1visiblity = prefs.getBoolean("TV1visibility", true);
                    btn2visibility = prefs.getBoolean("TV2visibility", true);

                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("TV1visibility", true);
                    editor.putBoolean("TV2visibility", true);
                    editor.commit();

                    tv1.setVisibility(View.VISIBLE);
                    tv2.setVisibility(View.VISIBLE);

                }
            });

            act2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Activity1.this, Activity2.class);
                    startActivity(intent);
                    finish();

                }
            });

            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Create or load preferences
                    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                    btn1visiblity = prefs.getBoolean("TV1visibility", true);

                    //save new boolean
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("TV1visibility", false);
                    editor.commit();
                    tv1.setVisibility(View.INVISIBLE);
                }
            });

            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Create or load preferences
                    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                    btn2visibility = prefs.getBoolean("TV2visibility", true);

                    //save new boolean
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("TV2visibility", false);
                    editor.commit();
                    tv2.setVisibility(View.INVISIBLE);
                }
            });

    }
    }

Activity2.class

     package com.key.hs.invisiblebuttons;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Hasan on 26.04.2017.
 */

public class  Activity2 extends AppCompatActivity {

    private boolean btn1visiblity, btn2visibility;
    private TextView tv1, tv2;
    private Button btn1, btn2, act1, reset;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2lay);

        tv1= (TextView)findViewById(R.id.tv1);
        tv2=(TextView)findViewById(R.id.tv2);
        btn1= (Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);
        act1=(Button)findViewById(R.id.act1);
        reset=(Button)findViewById(R.id.reset);

        //Create or load preferences
        final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
        btn1visiblity = prefs.getBoolean("TV1visibility", true);
        btn2visibility = prefs.getBoolean("TV2visibility", true);

        //if tvs invisible in activity1 make them visible in activity2
        if(!btn1visiblity){
            tv1.setVisibility(View.VISIBLE);
        }else{
            tv1.setVisibility(View.INVISIBLE);
        }
        if(!btn2visibility){
            tv2.setVisibility(View.VISIBLE);
        }else{
            tv2.setVisibility(View.INVISIBLE);
        }

        //resets booleans
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                btn1visiblity = prefs.getBoolean("TV1visibility", true);
                btn2visibility = prefs.getBoolean("TV2visibility", true);

                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("TV1visibility", true);
                editor.putBoolean("TV2visibility", true);
                editor.commit();
                tv1.setVisibility(View.INVISIBLE);
                tv2.setVisibility(View.INVISIBLE);

            }
        });


        //go to activity1
        act1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Activity2.this, Activity1.class);
                startActivity(intent);
                finish();

            }
        });

        //makes tv1 invible
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create or load preferences
                final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                btn1visiblity = prefs.getBoolean("TV1visibility", true);

                //save new boolean
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("TV1visibility", true);
                editor.commit();
                tv1.setVisibility(View.INVISIBLE);
            }
        });

        //makes tv2 invisible
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create or load preferences
                final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                btn2visibility = prefs.getBoolean("TV2visibility", true);

                //save new boolean
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("TV2visibility", true);
                editor.commit();
                tv2.setVisibility(View.INVISIBLE);
            }
        });



    }
}

activity1lay.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:text="example tv1"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv2"
        android:text="example tv2"
        android:layout_alignParentEnd="true"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:text="TV2 invisible"
        android:layout_alignParentBottom="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="TV1 invisible"
        android:layout_above="@id/btn2"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/act2"
        android:text="ACTIVITY2"
        android:layout_alignParentEnd="true"
        android:layout_centerInParent="true"></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/reset"
        android:text="RESET VISIBILITY"
        android:layout_alignParentStart="true"
        android:layout_centerInParent="true"/>

</RelativeLayout>

activity2lay.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:text="example tv1"
            android:layout_centerInParent="true"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv2"
            android:text="example tv2"
            android:layout_above="@id/tv1"
            android:layout_centerHorizontal="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn1"
            android:text="TV1 invisible"
            android:layout_above="@id/btn2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn2"
            android:text="TV2 invisible"
            android:layout_alignParentBottom="true"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/act1"
            android:text="ACTIVITY1"
            android:layout_alignParentEnd="true"
            android:layout_centerInParent="true"></Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/reset"
            android:text="RESET VISIBILITY"
            android:layout_alignParentStart="true"
            android:layout_centerInParent="true"/>
    </RelativeLayout>


来源:https://stackoverflow.com/questions/43553042/sharedpreferences-save-a-button-change-invisible-after-click-on-it-and-let-appea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!