【发布时间】:2014-09-15 08:06:37
【问题描述】:
我目前在我的 Android 应用程序中使用一个开关作为我的设置类中的一种选项。但是,当我将开关的状态从说 false 更改为 true,然后返回菜单,然后返回设置活动时,开关的状态会回到默认值。是否有一个关键行基本上保存了开关的状态,以便当用户返回它时,状态是相同的?我认为这个问题不需要代码,但如果您认为它有帮助,请告诉我,我会附上。谢谢!
这是我的活动代码:
<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="com.example.speedytext.Options" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="46dp"
android:layout_marginTop="48dp"
android:layout_toRightOf="@+id/textView1"
android:text="Switch" />
<Switch
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/switch1"
android:layout_below="@+id/switch1"
android:layout_marginTop="66dp"
android:text="Switch" />
</RelativeLayout>
这是我的java代码:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
public class Options extends ActionBarActivity {
private static Switch ding;
private Switch countdown;
public static boolean isDingChecked;
public static boolean isCountdownChecked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
ding = (Switch) findViewById(R.id.switch1);
ding.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something, the isChecked will be
// true if the switch is in the On position
isDingChecked = isChecked;
System.out.println(isDingChecked);
System.out.println(isDingChecked());
}
});
countdown = (Switch) findViewById(R.id.switch2);
countdown.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something, the isChecked will be
// true if the switch is in the On position
isCountdownChecked = isChecked;
System.out.println(isCountDownChecked());
}
});
isDingChecked = ding.isChecked();
isCountdownChecked= countdown.isChecked();
}
public static boolean isDingChecked()
{
return isDingChecked;
}
public static boolean isCountDownChecked()
{
return isCountdownChecked;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
-
一些代码真的很有帮助!
-
是的,我们找到了一些代码。您使用的是通常开关还是 switchPreference?只有 switchPreference 会保持它的状态,通常的 Switch 不会。
-
您应该将您的开关状态放入一个数组列表中,然后将它们保存在 savedInstanceState 或共享首选项中