【问题标题】:Android Switch Issue安卓交换机问题
【发布时间】: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 或共享首选项中

标签: java android


【解决方案1】:

onCreate() 中,使用您存储的“已检查”值调用setChecked(),将默认值设置为之前存储的值。

另外,考虑将设置存储到例如共享偏好。 static 变量不会在应用程序重新启动时持续存在,并且通常会使您的代码更难维护。

【讨论】:

  • 是的,我不知道为什么我没有想到这一点……常识。谢谢!
  • 那么现在的问题是什么?
  • 我认为是因为我没有添加共享偏好部分。如果我返回我的菜单活动并返回此选项活动,则开关的状态将丢失。让我尝试添加共享首选项部分!谢谢!
【解决方案2】:

共享偏好类

public class AppPreferences {

    private SharedPreferences appSharedPrefs;
    private Editor prefsEditor;

    public AppPreferences(Context context, String Preferncename) {
        this.appSharedPrefs = context.getSharedPreferences(Preferncename,
                Activity.MODE_PRIVATE);
        this.prefsEditor = appSharedPrefs.edit();
    }

    /****
     * 
     * getdata() get the value from the preference
     * 
     * */
    public String getData(String key) {
        return appSharedPrefs.getString(key, "");
    }

    /****
     * 
     * SaveData() save the value to the preference
     * 
     * */
    public void SaveData(String text, String Tag) {
        prefsEditor.putString(Tag, text);
        prefsEditor.commit();

    }


    public void clear()
    {

         prefsEditor.clear();
         prefsEditor.commit();
    }

}

在活动中使用共享偏好

AppPreferences appPref;

appPref = new AppPreferences(getApplicationContext(), "PREFS");

保存状态

当开关的状态改变时,将状态 true 或 false 保存到共享首选项中的标签

你可以用这行来保存状态

appPref.SaveData("Value", "Tag");

所以检查状态appPref.SaveData("true", "state");会是这样的

appPref.SaveData("false", "state"); 表示未检查状态

在您的活动的 oncreate 中从共享偏好中获取价值 使用

appPref.getData("state");
if(appPref.getData("state").equals("true"))
{
//set ur switch button to checked state
}
else if(appPref.getData("state").equals("false"))
{
//set ur switch button to unchecked state
}

然后检查结果字符串的真假 并相应地设置开关

【讨论】:

  • appPref.getData("state") 是否应该返回一个布尔值或字符串来表示真假?
  • string 将是返回类型,我将结果字符串与 equals 方法进行比较
  • 哦,我很抱歉没有看到编辑,因为我没有刷新页面。让我试试看它是否有效!
  • 所以你提到最后一部分应该在我的oncreate中。但是 AppPreferences appPref 呢? appPref = new AppPreferences(getApplicationContext(), "PREFS");和 appPref.SaveData("Value", "Tag");。这不也在同一个 OnCreate 中吗?
  • 你应该在 oncreate 方法中调用 appPref.getData("state") 之前初始化所有内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-07
  • 2018-07-03
  • 2014-02-19
  • 2011-06-12
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多