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