簡體   English   中英

檢查是否使用共享首選項選中單選按鈕

[英]check if radio button is checked using shared preferences

我可以在每次應用程序啟動時使用共享首選項和加載首選項保存選中的單選按鈕,但我想向單選按鈕添加一些代碼,即檢查單選按鈕是否被選中並執行一個操作,例如為每個收音機顯示吐司單獨的按鈕。每次活動開始時都不應該觸發該操作,因為我嘗試在特定時間段后使用 AlarmManager 設置通知,因此再次調用該方法會重置它

package com.example.radiogroup;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.app.Activity;
import android.content.SharedPreferences;

public class MainActivity extends Activity {

 RadioGroup radioGroup;
 TextView textCheckedID, textCheckedIndex;

 final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);

        textCheckedID = (TextView)findViewById(R.id.checkedid);
        textCheckedIndex = (TextView)findViewById(R.id.checkedindex);

        LoadPreferences();
    }

    OnCheckedChangeListener radioGroupOnCheckedChangeListener =
      new OnCheckedChangeListener(){

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

     RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
     int checkedIndex = radioGroup.indexOfChild(checkedRadioButton);

     textCheckedID.setText("checkedID = " + checkedId);
     textCheckedIndex.setText("checkedIndex = " + checkedIndex);
     SavePreferences(KEY_SAVED_RADIO_BUTTON_INDEX, checkedIndex);
    }};

 private void SavePreferences(String key, int value){
  SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
  SharedPreferences.Editor editor = sharedPreferences.edit();
  editor.putInt(key, value);
  editor.commit(); 
 }

 private void LoadPreferences(){
  SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
  int savedRadioIndex = sharedPreferences.getInt(KEY_SAVED_RADIO_BUTTON_INDEX, 0);
  RadioButton savedCheckedRadioButton = (RadioButton)radioGroup.getChildAt(savedRadioIndex);
  savedCheckedRadioButton.setChecked(true);
 }
}


<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/option1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Option 1" />
        <RadioButton
            android:id="@+id/option2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Option 2" />
        <RadioButton
            android:id="@+id/option3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Option 3" />
    </RadioGroup>

    <TextView
        android:id="@+id/checkedid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/checkedindex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我試過了

if(jRadioButton1.isSelected()){
        jTextField1.setText("Welcome");
    }
    else if(jRadioButton2.isSelected()){
        jTextField1.setText("Hello");
    }

但它不執行任何操作

RadioButton.isChecked(); 是你需要使用的。 如果選中返回true

private void LoadPreferences(){
  SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
  int savedRadioIndex = sharedPreferences.getInt(KEY_SAVED_RADIO_BUTTON_INDEX, 0);
  RadioButton savedCheckedRadioButton = (RadioButton)radioGroup.getChildAt(savedRadioIndex);
  if(!savedCheckedRadioButton.isChecked())
          savedCheckedRadioButton.setChecked(true);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM