记住点击的最后一个单选按钮

时间:2015-04-11 12:25:04

标签: android radio-button radio-group

假设一组4个单选按钮。用户点击第一个,然后他例如意识到他犯了一个错误,他点击了第四个。有没有办法记住最后一个单击并让应用程序忘记第一个被点击了?

1 个答案:

答案 0 :(得分:0)

以下是使用SharedPreferences的简单示例。比Sqlite更容易,更简单。

布局

<强> demo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/rank_radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkedButton="@+id/first_radio"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/first_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="First" />

        <RadioButton
            android:id="@+id/second_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second" />

        <RadioButton
            android:id="@+id/third_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Third" />

        <RadioButton
            android:id="@+id/fourth_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fourth" />
    </RadioGroup>


</LinearLayout>

活动

<强> DemoAppActivity.java

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

public class DemoAppActivity extends ActionBarActivity implements
        OnCheckedChangeListener {

    private RadioButton mFirstRadioButton = null;
    private RadioButton mSecondRadioButton = null;
    private RadioButton mThirdRadioButton = null;
    private RadioButton mFourthRadioButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_app);

        initializeRadioGroup();
    }

    private void initializeRadioGroup() {

        mFirstRadioButton = (RadioButton) findViewById(R.id.first_radio);
        mSecondRadioButton = (RadioButton) findViewById(R.id.second_radio);
        mThirdRadioButton = (RadioButton) findViewById(R.id.third_radio);
        mFourthRadioButton = (RadioButton) findViewById(R.id.fourth_radio);

        // Fetching last checked position in preferences
        int lastCheckedPosition = PreferenceManager
                .getDefaultSharedPreferences(this).getInt("last_checked", 0);

        Log.d("TAG", "Fetching last saved position " + lastCheckedPosition);

        switch (lastCheckedPosition) {
        case 1:
            mFirstRadioButton.setChecked(true);
            break;
        case 2:
            mSecondRadioButton.setChecked(true);
            break;
        case 3:
            mThirdRadioButton.setChecked(true);
            break;
        case 4:
            mFourthRadioButton.setChecked(true);
            break;
        default:
            mFirstRadioButton.setChecked(true);
            break;
        }

        mFirstRadioButton.setOnCheckedChangeListener(this);
        mSecondRadioButton.setOnCheckedChangeListener(this);
        mThirdRadioButton.setOnCheckedChangeListener(this);
        mFourthRadioButton.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int newCheckedPos = 0;

        if (isChecked) {
            switch (buttonView.getId()) {
            case R.id.first_radio:
                newCheckedPos = 1;
                break;
            case R.id.second_radio:
                newCheckedPos = 2;
                break;
            case R.id.third_radio:
                newCheckedPos = 3;
                break;
            case R.id.fourth_radio:
                newCheckedPos = 4;
                break;
            }
        }

        if (newCheckedPos > 0) {
            Log.d("TAG", "Saving new checked position " + newCheckedPos);

            // Saving checked position in preferences
            PreferenceManager.getDefaultSharedPreferences(this).edit()
                    .putInt("last_checked", newCheckedPos).commit();
        }

    }

}