覆盖设置

时间:2015-12-06 16:42:57

标签: android save sharedpreferences settings

当我的应用程序打开时,您必须填写一些行,例如您的姓名,重量和长度,然后通过按下浮动操作按钮(它将其保存到名为Util的活动)来保存。之后,我想让用户在单击按钮时在settingsFragment中编辑这些设置。我有一些代码,但它不起作用,我不知道如何解决这个问题,有人可以帮助我吗?

以下是我第一个屏幕的代码:

fab.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       String length = inputLength.getText().toString();
                       String weight = inputWeight.getText().toString();
                       int Length = Integer.parseInt(length);
                       int Weight = Integer.parseInt(weight);
                       Util.get().setName(inputName.getText().toString());
                       Util.get().setLength(Length);
                       Util.get().setWeight(Weight);
                       Integer lengthValue = Integer.parseInt(length);
                       Integer weightValue = Integer.parseInt(weight);

                       SharedPreferences prefs = getSharedPreferences("be.ugent.MyApplication", MODE_PRIVATE);
                       prefs.edit().putString("name", inputName.getText().toString()).apply();
                       prefs.edit().putInt("weight", weightValue).apply();
                       prefs.edit().putInt("length", lengthValue).apply();

                       Intent main = new Intent(SplashScreen.this, MainActivity.class);
                       startActivity(main);
                       overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                   }
               });

这是我的settingsFragment

Button save = (Button)settingsView.findViewById(R.id.save);

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String length = inputLength.getText().toString();
            String weight = inputWeight.getText().toString();
            int Length = Integer.parseInt(length);
            int Weight = Integer.parseInt(weight);
            Util.get().setName(inputName.getText().toString());
            Util.get().setLength(Length);
            Util.get().setWeight(Weight);
            Integer lengthValue = Integer.parseInt(length);
            Integer weightValue = Integer.parseInt(weight);

            SharedPreferences prefs = getActivity().getSharedPreferences("be.ugent.MyApplication", Context.MODE_PRIVATE);
            prefs.edit().putString("name", inputName.getText().toString()).commit();
            prefs.edit().putInt("weight", weightValue).commit();
            prefs.edit().putInt("length", lengthValue).commit();
            Toast.makeText(getActivity().getApplicationContext(), "Gegevens opgeslagen",
                    Toast.LENGTH_LONG).show();

        }

    });

    inputName.setText(Util.get().getName());

干杯!

0 个答案:

没有答案