SharedPreferences中的单选按钮?

时间:2012-01-18 12:17:54

标签: android

我有一个带有textfield,radiofield和numberfield的基本信息表单

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="Your Information"
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:gravity="center"/>

</TableRow>
<View 
        android:background="@drawable/barbell"
        android:layout_width="fill_parent"
        android:layout_height="30dip"/>


<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nickname: " 
        android:layout_weight="1" 
        android:textSize="18sp" 
        android:textColor="#333333"/>
    <EditText
        android:id="@+id/infoname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="3 to 10 characters" 
        android:layout_weight="1"/>

</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender: " 
        android:layout_weight="1" 
        android:layout_gravity="center_vertical" 
        android:textSize="18sp" 
        android:textColor="#333333"/>
    <RadioGroup
        android:id="@+id/inforadioGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_weight="1">
        <RadioButton
            android:id="@+id/inforadioM"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Male"/>
        <RadioButton
        android:id="@+id/inforadioF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />
    </RadioGroup>


</TableRow>

<TableRow
    android:id="@+id/tableRow4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Age: " 
        android:layout_weight="1" android:textSize="18sp" android:textColor="#333333"/>

    <EditText
        android:id="@+id/infoage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number" 
        android:hint="5 to 99" 
        android:layout_weight="1"/>

</TableRow>

我使用sharedpreference及其编辑器传递字符串和数字。
但我非常需要教程或单选按钮中的初学者?
是否有可能不创建preferences.xml(我不想搞砸)

2 个答案:

答案 0 :(得分:0)

您可以使用android的默认sqlite数据库来存储数据。

为了让您开始使用单选按钮,这是来自android

的一个很好的参考

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/RadioGroup1.html

答案 1 :(得分:0)

RadioGroup有方法getCheckedRadioButtonId()check(int id)。所以,如果你想保存视图状态,你可以简单地存储所选单选按钮的id,这是一个int,如果你想再次加载你的视图,你可以调用check方法并将保存的int传入再次选择单选按钮。