Android Studio 如何创建一个按钮,点击后返回到一个活动,其中已经把值在sharedPref和编辑这些值?

f8rj6qna  于 2023-11-21  发布在  Android
关注(0)|答案(1)|浏览(199)

如何创建一个按钮,点击后返回到一个活动,其中已经把值在sharedPref和编辑这些值?我试图使一个应用程序,用户输入他的身高,体重,年龄和选择从收音机组收音机-男性或女性,并点击后“下一步”按钮,它会进入主菜单,在那里它显示用户刚刚输入的值。我做了一个if,它会看这些值是否已经输入过,如果是,它只是跳过活动,它直接进入主菜单。我想做一个按钮编辑,当它被点击回到活动,用户可以编辑他之前输入的值。我张贴我的主活动和我的主菜单下面的代码,P.S.我是新的编码,我不理解一切,这就是为什么我问一个问题,很想看到一个简单的详细解释的步骤,做我想在代码中的东西。谢谢大家!

public class MainActivity extends AppCompatActivity {
EditText enterHeight, enterWeight, enterAge;

private double height;
private double weight;
private double age;
String selectedRadiobut;
Button button;
RadioButton radioMale, radioFemale;
RadioGroup radioGroup;
SharedPreferences sp;

private boolean heightIsValid() {

    if (enterHeight.getText().toString().length() == 0) {
        enterHeight.setError("Cant be empty");
        return false;
    }

    double height = Double.valueOf(enterHeight.getText().toString());

    if (height <= 0) {

        enterHeight.setError("Cant be zero");
        return false;
    }
    if (height <= 40 || height > 250) {

        enterHeight.setError("Cant be less than 40 cm or more than 250 cm");
        return false;
    }
    return true;
}

private boolean weightIsValid() {
    if (enterWeight.getText().toString().length() == 0) {
        enterWeight.setError("Cant be empty");
        return false;
    }

    double weight = Double.valueOf(enterWeight.getText().toString());

    if (weight <= 0) {

        enterWeight.setError("Cant be zero");
        return false;
    }
    if (weight > 600) {

        enterWeight.setError("Cant be more than 600 kg");
        return false;
    }
    return true;
}

private boolean ageIsValid() {
    if (enterAge.getText().toString().length() == 0) {
        enterAge.setError("Cant be empty");
        return false;
    }

    double age = Double.valueOf(enterAge.getText().toString());

    if (age <= 0) {

        enterAge.setError("Cant be zero");
        return false;
    }
    if (age > 116) {

        enterAge.setError("Cant be older than 116 years");
        return false;
    }
    return true;
}

private boolean genderIsSelected() {

    if (radioGroup.getCheckedRadioButtonId() == -1) { //checking if the user has selected a option using id
        Toast toast = Toast.makeText(this, "Please select gender", Toast.LENGTH_SHORT);
        toast.show();
        return false;
    }
    if (radioMale.isChecked()) {
        selectedRadiobut = radioMale.getText().toString();
    } else if (radioFemale.isChecked()) {
        selectedRadiobut = radioFemale.getText().toString();
    }
    return true;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_main);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    sp = getSharedPreferences("MyUserPrefs", Context.MODE_PRIVATE);

    if (sp.contains("height") && sp.contains("weight") && sp.contains("age") && sp.contains("gender")) {
        // Values are stored, navigate to MainMenu directly
        Intent intent = new Intent(MainActivity.this, MainMenu.class);
        startActivity(intent);
    } else {
        radioMale = (RadioButton) findViewById(R.id.radioMale);
        radioFemale = (RadioButton) findViewById(R.id.radioFemale);
        button = (Button) findViewById(R.id.button);
        enterHeight = (EditText) findViewById(R.id.enterHeight);
        enterWeight = (EditText) findViewById(R.id.enterWeight);
        enterAge = (EditText) findViewById(R.id.enterAge);
        radioGroup = (RadioGroup) findViewById(R.id.radioSex);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (heightIsValid() & weightIsValid() & ageIsValid() & genderIsSelected()) {

                    height = Double.valueOf(enterHeight.getText().toString());
                    weight = Double.valueOf(enterWeight.getText().toString());
                    age = Double.valueOf(enterAge.getText().toString());

                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("height", String.valueOf(Integer.valueOf((int) height)));
                    editor.putString("weight", String.valueOf(Integer.valueOf((int) weight)));
                    editor.putString("age", String.valueOf(Integer.valueOf((int) age)));
                    editor.putString("gender", String.valueOf(selectedRadiobut));
                    editor.commit();

                    InputMethodManager imm = (InputMethodManager) getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    Intent intent = new Intent(MainActivity.this, MainMenu.class);
                    startActivity(intent);
                }
            }
        });
    }
}

字符串
主菜单:
public class Uncategorized {
按钮btnMC、btnFN、btnRec、btnGuides、btnEdit;

public void btnStepClick(View view) {
    Intent intent = new Intent(this, StepCounter.class);
    startActivity(intent);
}
public void btnCalClick(View view) {
    Intent intent = new Intent(this, MacrosCalculator.class);
    startActivity(intent);
}
public void btnRecClick(View view){
    Intent intent = new Intent(this, Recipes.class);
    startActivity(intent);
}
public void btnEditClick(View view){
    Log.d("MainMenu", "Edit button clicked");
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_main_menu);
    TextView age1 , height1 , weight1 , gender1;

    age1 = (TextView) findViewById(R.id.age1);
    height1 = (TextView) findViewById(R.id.height1);
    weight1 = (TextView) findViewById(R.id.weight1);
    gender1 = (TextView) findViewById(R.id.gender1);
    btnMC = (Button) findViewById(R.id.btnMC);
    btnFN = (Button) findViewById(R.id.btnFN);
    btnRec = (Button) findViewById(R.id.btnRec);
    btnGuides = (Button) findViewById(R.id.btnGuides);
    btnEdit = (Button) findViewById(R.id.btnEdit);

    SharedPreferences sp = getApplicationContext().getSharedPreferences("MyUserPrefs", Context.MODE_PRIVATE);
    String height = sp.getString("height" , "");
    String weight = sp.getString("weight" , "");
    String age = sp.getString("age" , "");
    String gender= sp.getString("gender", "");

    gender1.setText("Gender: " + gender);
    age1.setText("Age: " + age);
    height1.setText("Height(cm): " + height);
    weight1.setText("Weight(kg): " + weight);

}


}
我想创建一个按钮编辑,当它被点击回到主活动,但当它被点击,应用程序崩溃。

jslywgbw

jslywgbw1#

我找到解决办法了!我必须在单击按钮后删除sharedprefs中的值。因此,在buttonClick中,我必须放置sharedprefs,然后是sp.edit().remove(“Value”).commit()。我在示例中的值是4,因此我放置了其中的4个,如下所示:)。

public void btnEditClick(View view){
        Log.d("MainMenu", "Edit button clicked");
        SharedPreferences sp = getSharedPreferences("MyUserPrefs", Context.MODE_PRIVATE);
        sp.edit().remove("height").commit();
        sp.edit().remove("weight").commit();
        sp.edit().remove("age").commit();
        sp.edit().remove("gender").commit();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

字符串

相关问题