我是新来的。我是法国人,所以也许我的英语不是很好,对不起。
我是Android开发的初学者,我必须创建一个应用程序来完成我的学习。
我有一个名为VoeuxActivity.java的活动编号,带有8个按钮(和8个TextViews),它们在开始时都是可见的,当用户点击其中一个按钮时,由INVISIBLE改变(用户无法看到按钮)点击之后),当我退出应用程序并再次回到我的应用程序时,由于SharedPreferences和此论坛的成员,该按钮始终是不可见的。但我想现在当我点击这个名为“totoB”的按钮时,它将是不可见的,另一个按钮在另一个名为PersoActivity.java的活动2上变为可见,因为当我选择一个新角色时,第一个活动用于解锁一些被锁定的角色。活动1,它将在活动1中不可见,并将在活动2上可见,以便为战斗选择2个字符(这就是为什么有一个布尔名称“isClicked”)但我尝试使用SharedPreferences保持按钮可见第二项活动,但它根本不起作用。 当一个新角色解锁后,如果我退出应用程序并再次回到我的应用程序中,新角色解锁不会保存为可见,并且他再次看不见但我希望他总是可以通过SharedPreferences看到。我在第一个活动上使用相同的按钮发布了一个法庭代码,然后在第二个活动上发布(我尝试和第一个活动一样,但我的解决方案无效),也许你可以帮我解决我的问题。
第一个活动的代码可以起作用并保存更改:
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);
}
});
}
我尝试为第二个活动做同样的事情,但这次不能正常工作,不会保存更改。
公共类PersoActivity扩展了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);
}}}
如何在第二个活动中将Button和TextView的更改从Visible保存到Invisible? 非常感谢你,如果有人可以帮助我,因为我真的不知道为什么它根本不起作用。 吉格斯
答案 0 :(得分:1)
我制作了一个示例应用并对其进行了测试。无论您进行哪些活动更改,它都会保存文本视图的最后状态(可见或不可见)。即使退出并返回应用程序,它也会加载最后一个状态。
清单文件
<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>