Java 如何保留android应用程序imageButton';重新启动、按后退和更改方向后的s onClick状态
我正在制作一个远程控制灯泡的应用程序。 因此,我要求应用程序自动保存以前的状态(变量、onlick按钮等),并在我重新启动应用程序后恢复。 例如,如果我按下灯泡(在我的代码中,它会在单击时更改图片),图片会更改,所以当我关闭应用程序并重新打开它时,显示的图像应该更改一个 这是我的密码Java 如何保留android应用程序imageButton';重新启动、按后退和更改方向后的s onClick状态,java,android,Java,Android,我正在制作一个远程控制灯泡的应用程序。 因此,我要求应用程序自动保存以前的状态(变量、onlick按钮等),并在我重新启动应用程序后恢复。 例如,如果我按下灯泡(在我的代码中,它会在单击时更改图片),图片会更改,所以当我关闭应用程序并重新打开它时,显示的图像应该更改一个 这是我的密码 package room.bt4u.com.roomcontrol; import android.media.MediaPlayer; import android.net.Uri; import android
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'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'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>
您可以使用SharedPref在屏幕方向更改或后退时使用任何键保存数据,也可以使用此键再次获取数据。您可以使用SharedPref在屏幕方向更改或后退时使用任何键保存数据,也可以使用此键再次获取数据。您可以使用共享引用: 参考以下信息: 您可以使用
SharedReferences
在手机上保存一些设置数据
在您的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");
这样,您就拥有了数据,可以在活动中使用它们您可以使用SharedReferences:
Try this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleSound= MediaPlayer.create(this, R.raw.z);
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
Boolean c = sharedPreferences.getBoolean("clicked");
if(c){
aButton.setImageResource(R.drawable.on);
}
else {
aButton.setImageResource(R.drawable.off);
}
}
public void buttonClick(View v) {
switch (choose){
case 1:ImageButton aButton = (ImageButton) v;
aButton.setImageResource(R.drawable.on);
toggleSound.start();
choose++;
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("clicked", true);
editor.commit();
break;
case 2:ImageButton bButton = (ImageButton) v;
bButton.setImageResource(R.drawable.off);
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("clicked", false);
editor.commit();
toggleSound.start();
choose--;
break;
}
参考以下信息:
您可以使用SharedReferences
在手机上保存一些设置数据
在您的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");
这样,您就拥有了数据,并且可以在活动中使用它们如果您没有与灯泡进行双向通信,那么将状态保存到
共享参考
似乎是最好的方法
Try this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleSound= MediaPlayer.create(this, R.raw.z);
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
Boolean c = sharedPreferences.getBoolean("clicked");
if(c){
aButton.setImageResource(R.drawable.on);
}
else {
aButton.setImageResource(R.drawable.off);
}
}
public void buttonClick(View v) {
switch (choose){
case 1:ImageButton aButton = (ImageButton) v;
aButton.setImageResource(R.drawable.on);
toggleSound.start();
choose++;
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("clicked", true);
editor.commit();
break;
case 2:ImageButton bButton = (ImageButton) v;
bButton.setImageResource(R.drawable.off);
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("clicked", false);
editor.commit();
toggleSound.start();
choose--;
break;
}
您可以从活动
或片段
生命周期方法中的共享参考
检索数据(onConfigurationChanged()
,onResume()
,onStart()
)
如果您没有与灯泡进行双向通信,将状态保存到
共享参考
似乎是最好的方法
您可以从活动
或片段
生命周期方法中的共享参考
检索数据(onConfigurationChanged()
,onResume()
,onStart()
)
当您手动按下该按钮时,请在sharedpref中保存一些布尔值;当您重新创建或更改方向时,请检查该布尔值,并在我按下Bubble(白色)时相应地设置值它变为黄色,因此如果我的应用程序被关闭或方向发生变化,我希望灯泡自动显示黄色,而无需任何单击。默认情况下,灯泡为白色或黄色?默认图像为白色,当单击时,灯泡变为黄色图像,因此它将显示默认值以进行更改。状态更改后,当您返回活动检查时,保存任何值值和设置值当您手动按此键时,在sharedpref中保存一些布尔值,当您重新创建或更改方向时,请在我按bulb(白色)时检查该布尔值并相应设置值它变为黄色,因此如果我的应用程序被关闭或方向发生变化,我希望灯泡自动显示黄色,而无需任何单击。默认情况下,灯泡为白色或黄色?默认图像为白色,当单击时,灯泡变为黄色图像,因此它将显示默认值以进行更改。状态更改后,当您返回活动检查时,保存任何值值和设置值当我按下bulb(白色)时,它变为黄色,因此如果我的应用程序被关闭或方向改变,我希望灯泡自动显示黄色,而无需任何单击当我按下bulb(白色)时,它变为黄色,因此如果我的应用程序被关闭或方向改变,我希望灯泡自动显示黄色,而无需任何单击Remove GetActivity。我在研究碎片。在活动中无需getActivityi声明图像按钮邻接;globallyalso在getBoolean(“STRING”,false)中应该有一个true或false,它显示了errow,所以我将默认值falseaButton=findViewById(R.id.YOUBUTTONID);粘贴YOUBUTTONID并将此行放在setContentViewdefault false下方即可。它应该在那里。删除GetActivity。我在研究碎片。在活动中无需getActivityi声明图像按钮邻接;globallyalso在getBoolean(“STRING”,false)中应该有一个true或false,它显示了errow,所以我将默认值falseaButton=findViewById(R.id.YOUBUTTONID);粘贴YOUBUTTONID并将此行放在setContentViewdefault false下方即可。它应该在那里。