gpt4 book ai didi

java - 如何在重新启动、按回键和更改方向后保留 Android 应用程序 imageButton onClick 状态

转载 作者:行者123 更新时间:2023-12-02 03:52:47 25 4
gpt4 key购买 nike

我正在制作一个远程控制灯泡的应用程序。所以我要求应用程序自动保存以前的状态(变量、单击按钮等),并在我重新启动应用程序后恢复它。例如,如果我按下灯泡(在我的代码中,它会在单击时更改图片)并且图片发生变化,因此当我关闭应用程序并重新打开它时,显示的图像应该更改。

这是我的代码

package room.bt4u.com.roomcontrol;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
public class MainActivity extends AppCompatActivity {
ImageButton ib;
MediaPlayer toggleSound;
ImageButton aButton,aButton2;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleSound=MediaPlayer.create(this, R.raw.z);
aButton = (ImageButton) findViewById(R.id.imageButton);
aButton2 = (ImageButton) findViewById(R.id.imageButton2);
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
Boolean c = sharedPreferences.getBoolean("clicked", false);
Boolean d = sharedPreferences.getBoolean("clicked2",false);

if(c) {
aButton.setImageResource(R.drawable.on);
}
else {
aButton.setImageResource(R.drawable.off);

}
if(d){

aButton2.setImageResource(R.drawable.on);
}
else {

aButton2.setImageResource(R.drawable.off);
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}


public void buttonClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
Boolean c = sharedPreferences.getBoolean("clicked",false);
if (!c) {
aButton.setImageResource(R.drawable.on);
toggleSound.start();
sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("clicked", true);
editor.commit();
}
if(c){
aButton.setImageResource(R.drawable.off);
sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor = sharedPreferences.edit();
editor.putBoolean("clicked", false);
editor.commit();
toggleSound.start();

}
}
public void buttonClick2(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
Boolean d = sharedPreferences.getBoolean("clicked",false);
if (!d) {
aButton2.setImageResource(R.drawable.on);
toggleSound.start();
sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("clicked2", true);
editor.commit();
}
if(d){
aButton2.setImageResource(R.drawable.off);
sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor = sharedPreferences.edit();
editor.putBoolean("clicked2", false);
editor.commit();
toggleSound.start();

}
}

这是 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="room.bt4u.com.roomcontrol.MainActivity"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROOM NO. 1046"
android:textSize="45sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:textColor="#0786e7"
android:id="@+id/textView"
android:includeFontPadding="false"
android:gravity="center_horizontal"
/>


<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_below="@+id/textView"
android:src="@drawable/off"
android:layout_marginTop="35dp"
android:layout_alignParentLeft="true"
android:background="#01FFFFFF"
android:onClick="buttonClick"/>

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton2"
android:layout_alignTop="@+id/imageButton"
android:layout_below="@+id/textView"
android:src="@drawable/off"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#01FFFFFF"
android:onClick="buttonClick2"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Swarnveer&apos;s"
android:id="@+id/textView2"
android:layout_below="@+id/imageButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:textColor="#f20606"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sajal&apos;s"
android:id="@+id/textView3"

android:layout_below="@+id/imageButton2"
android:layout_alignRight="@+id/imageButton2"
android:layout_alignEnd="@+id/imageButton2"
android:layout_marginRight="35dp"
android:layout_marginEnd="35dp"
android:textStyle="bold"
android:textColor="#f20606"/>

</RelativeLayout>

最佳答案

您可以使用 SharedPreferences:

引用here对于信息:您可以使用 SharedPreferences 在手机上保存少量设置数据。

onPause() 中,使用以下代码:

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
prefs.edit().putString("myKey", "myColorState").apply();

这样,“myColorState”的设置就保存在“myKey”键下。

onResume() 中,您可以使用以下方法检索该值

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
String myColor = prefs.getString("myKey", "defaultValue");

通过这种方式,您可以获得数据并可以在 Activity 中使用它们

关于java - 如何在重新启动、按回键和更改方向后保留 Android 应用程序 imageButton onClick 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35742453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com