Android CheckBox снимите флажок - PullRequest
0 голосов
/ 17 мая 2019

Я хочу добавить проверку сохранения в приложении Android.Есть один CheckBox, два EditText.Но все проверки не влияют, что-то не так с моим кодом сохранения?

@Override
public void onClick(View v) {
String errString = SaveCheck();
}

private String SaveCheck() {
//et_other:other transportation
//CheckCar:car
//et_mileage:mileage


CheckBox CBC = (CheckBox) findViewById(R.id.CheckCar);

if (et_other.getText() != null && !CBC.isChecked() && et_mileage.getText().toString().startsWith("0.0")) {
float temp = Float.parseFloat(et_mileage.getText().toString());
if (temp > 0.0)
    return " Fill in other transportation and the car is not checked, mileage must 0";
}

if (et_other.getText() != null && CBC.isChecked() && et_mileage.getText().toString().startsWith("0.0")) {
float temp = Float.parseFloat(et_mileage.getText().toString());
if (temp == 0.0)
    return " Fill in other transportation and check the car, mileage mustn't 0";
}
}

1 Ответ

0 голосов
/ 17 мая 2019

вы можете сохранить свое состояние флажка в sharedPreference, а затем использовать это состояние там, где вы хотите, даже если вы закроете приложение и снова откроете, вы также можете получить последнее проверенное состояние.Это очень просто: вам нужно создать класс пользовательских настроек, установить некоторые методы получения и установки и использовать их во всем проекте:

Ваш класс настройки; -

public class AppPrefrences {

            private static SharedPreferences mPrefs;
            private static SharedPreferences.Editor mPrefsEditor;

            public static boolean isCheckBoxChecked(Context ctx) {
                mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
                return mPrefs.getBoolean("checkBoxState", true);
            }

            public static void setCheckBoxChecked(Context ctx, Boolean value) {
                mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
                mPrefsEditor = mPrefs.edit();
                mPrefsEditor.putBoolean("checkBoxState", value);
                mPrefsEditor.commit();
            }

        }

и использовать этоустановите флажок, затем сохраните состояние в этом методе: -

AppPreference.setCheckBoxChecked(this, your checkbox state in true or false);

, а когда вам нужно это состояние, вызовите этот метод: -

if(isCheckBoxChecked(this)){
  // this is chaced state
}else{
  // this is not chacked state
}
...