如何在共享偏好中使用无线电组?

时间:2017-12-30 12:57:04

标签: android sharedpreferences radio-group

我正在进行移动项目,我使用共享偏好设置来保存用户点击以供日后使用。其中一个是单选按钮,经过一些研究,我发现要获得点击的特定单选按钮,我必须使用无线电组。当我尝试这个时,它只允许我点击一个单选按钮,当我点击它时,应用程序被迫停止。我创建了一个名为Preference.java的类来设置和获取所有用户点击,包括单选按钮。

MainActivity.java

    public class MainActivity extends AppCompatActivity  {

    private Preferences preferences;
    final String KEY_SAVED_CAR_INDEX = "KEY_SAVED_CAR_INDEX";
    final String KEY_SAVED_MAN_INDEX = "KEY_SAVED_MAN_INDEX";
    Button clickbtn;
    CheckBox start_from_home;
    RadioButton carchecked, manchecked;
    RadioGroup radioGroup;
    Spinner spinner ;
    EditText ed1;
    int selectedPosition;

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

        ed1 = (EditText) findViewById(R.id.edittext1);

        spinner=(Spinner)findViewById(R.id.spinner_hours);
        selectedPosition= spinner.getSelectedItemPosition();

        clickbtn = (Button) findViewById(R.id.click_btn);
        clickbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                init();
                Intent intent1 = new Intent(getApplicationContext(), Main2Activity.class);
                startActivity(intent1);
            }

        });

        start_from_home =(CheckBox) findViewById(R.id.start_check);
        start_from_home.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(start_from_home.isChecked()) {
                    preferences = new Preferences(MainActivity.this);
                    preferences.setstartfromhome(b);
                }
            }
        });

        radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
//        radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
                RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
                int checkedIndex = radioGroup.indexOfChild(checkedRadioButton);
                int radioButtonID = radioGroup.getCheckedRadioButtonId();

                preferences = new Preferences(MainActivity.this);

                if(radioButtonID == R.id.car_radio) {
                    preferences.settransportationchecked(KEY_SAVED_CAR_INDEX, checkedIndex);
                }
                else if (radioButtonID== R.id.man_radio)
                    preferences.settransportationchecked(KEY_SAVED_MAN_INDEX,checkedIndex);
}
        });

    }
private void init() {

        preferences = new Preferences(MainActivity.this);

        preferences.setcurrentlocation(ed1.getText().toString());
     preferences.setspinnerhours(selectedPosition);
    }


}

activity_main.xml中

<RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@+id/edittext1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <RadioButton
            android:id="@+id/car_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/click_btn"
            android:layout_marginTop="25dp"
            android:text="Car" />



        <RadioButton
            android:id="@+id/man_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Walking"
            android:onClick="onRadioButtonClicked"
            android:layout_below="@+id/car_radio"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="51dp" />
    </RadioGroup>

Preference.java

public void settransportationchecked(String key, int value){
    SharedPreferences sharedPreferences = context.getSharedPreferences("preferences", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
}

1 个答案:

答案 0 :(得分:0)

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    // Summary: generates processes for "n_total" notepads allowing for a maximum of "n_cpus"
    //          notepads at each time. Every time a notepad closes another appears until all are run.
    //          A single variable "p" instantiates the class "Process" and the event "p.Exited"
    //          updates the number of running processes "n_running".

    static int n_running = 0; // number of notepads running each time
    static void Main()
    {
        int n_cpus = 3;
        int n_total = 3 * n_cpus;
        int i_run = 0;

        while (i_run < n_total) // Process generating routine until all are run
        {
            if (n_running < n_cpus) // Only a maximum of n_cpus running at each time
            {
                n_running++;
                i_run++;

                Process p = new Process(); // A new object per process
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.Arguments = "/c notepad.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.EnableRaisingEvents = true;
                p.Exited += p_Exited; // Is this associated with a particular object "p", right?
                p.Start();
            }
            else Thread.Sleep(1000); // Waits 1s before checking for new terminated processes
        }
    }

    static private void p_Exited(object sender, EventArgs e)
    {
        n_running--; // Updates the number of active processes. Triggers new future processes
    }
}

请将它从布局文件中删除,然后尝试它似乎没有实现。