我有两个活动,ActivityA
和ActivityB
。 ActivityA
有一个复选框,ActivityB
有一个按钮。当我单击ActivityA
中的Checkbox
时,我希望ActivityB
中的按钮不可见。我希望它在我抬起刻度时可见。最重要的是,我想用SharedPreferences
保存它.所以我想退出程序并重新进入我做的最后一个过程。有人可以帮我吗?提前谢谢。
> activity_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context=".MainActivity"
android:orientation="vertical">
<CheckBox
android:id="@+id/chk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FF5722"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="25dp"
android:text="Show Button"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="#000"
android:textSize="14sp"
android:textColor="#fff"
android:textAllCaps="false"
android:text="Next Screen"/>
</LinearLayout>
在活动 A 中:您可以使用共享首选项实现。
步骤1:将以下代码放在您的A活动中,是的,btn用于检查下一个屏幕按钮是否隐藏
SharedPreferences sharedPreferences = getSharedPreferences("ButtonPrefs", MODE_PRIVATE);
@SuppressLint("CommitPrefEdits") final SharedPreferences.Editor editor=sharedPreferences.edit();
chk=findViewById(R.id.chk);
btn=findViewById(R.id.btn);
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
editor.putBoolean("isShow",false);
editor.apply();
}
else
{
editor.putBoolean("isShow",true);
editor.apply();
}
}
});
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
activity_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:orientation="vertical">
<android.support.v7.widget.AppCompatButton
android:layout_margin="50dp"
android:id="@+id/btnshoworhide"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="#000"
android:visibility="gone"
android:textSize="14sp"
android:textColor="#fff"
android:textAllCaps="false"
android:text="show me or not"/>
</LinearLayout>
第 1 步:将以下代码放入您的 B 活动中
boolean showornot;
btnshoworhide=findViewById(R.id.btnshoworhide);
SharedPreferences sharedPreferences = getSharedPreferences("ButtonPrefs", MODE_PRIVATE);
showornot=sharedPreferences.getBoolean("isShow",false);
if(!showornot)
{
btnshoworhide.setVisibility(View.GONE);
}
else
{
btnshoworhide.setVisibility(View.VISIBLE);
}
它的简单和漂亮我成功运行此代码;)
您可以通过 Intent 将数据从活动 A 传递到 B,
Intent intent = new Intent(ctx, B.class);
intent.putExtra("checkbox_result", "true");
startActivity(intent);
并在 B 活动中获取数据并将此字符串保存在共享首选项中:
String check= getIntent().getStringExtra("checkbox_result");
您可以使用可在整个应用程序中访问的通用共享首选项。 将数据从活动 A 放在上面,然后从活动 B 读取。
从像这样的两个活动中获取它的实例。
SharedPreference sp = PreferenceManager.getDefaultSharedPreference(context);
在其上保存的数据,
SharedPreference.Editor editor = sp.getEditor();
editor.putBoolean(MY_BOL, checkboxValue).apply();
从您以前保存到的相同首选项中读取保存的值,以与保存时相同的方式获取它的实例,如上所示,获取值,在活动 N 的 onCreate(( 方法中执行此操作。
boolean bol = sharedPreference. getBoolean(MY_BOL, false);
button.setVisibility(bol);