0

我每次使用选项卡按钮时都会膨胀具有新复选框的布局,我如何保存这些复选框的状态和(键、值) ,当我再次膨胀时检索这些状态,共享首选项可以做到这一点,或者我应该这样做使用 SQLite 数据库?

这是我正在谈论的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:id="@+id/checkBox"
    style="?android:attr/starStyle"
    android:layout_alignBottom="@+id/teamTextView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/teamTextView"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/LogoImageView"
    android:layout_toLeftOf="@+id/checkBox"
    android:layout_toStartOf="@+id/checkBox"
    android:gravity="center"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="2dp"
    android:background="#d3d3d3" >
</LinearLayout>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/LogoImageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>
4

1 回答 1

0

用于SharedPrefernces保存和检索CheckBox状态

SharedPrefernces sharedPrefernces = getSharedPreferences(<SharedPreferencesName>, <Mode>);

CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
boolean checkBoxState = checkBox.getChecked();

// Save state in SharedPreferences
SharedPrefernces.Editor editor = sharedPrefernces.edit();
editor.putBoolean(<Key>, state).commit();

// Retrieve CheckBox state from SharedPreferences
boolean checkBoxState = sharedPrefernces.getBoolean(<Key>, <DefaultValue>);

请注意,getSharedPreferences如果您不在上下文中调用它,则需要使用上下文调用Activity

于 2015-10-22T17:23:56.133 回答