编辑文本到字符串不保存键入的内容

时间:2014-05-31 02:02:26

标签: android string android-edittext sharedpreferences gettext

在广泛试图找出为什么我的SharedPreferences总是空白或从未创建之后。我的结论是,因为字符串的编辑文本没有做任何事情,但我不知道它为什么不做任何事情。

这是代码

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class Friday extends Fragment{


    private String GBand, BBand, ADV1Band, ADV2Band, CBand, FBand;

    @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
        setHasOptionsMenu(true);



        View view = inflater.inflate(R.layout.friday, container, false);

        TextView GValue = (TextView) view.findViewById(R.id.bandText111111);
        GValue.setText( "G Band" );

        EditText GEdit = (EditText) view.findViewById(R.id.editText111111);
        GBand = GEdit.getText().toString();

        TextView BValue = (TextView) view.findViewById(R.id.bandText222222);
        BValue.setText( "B Band" );

        EditText BEdit = (EditText) view.findViewById(R.id.editText222222);
        BBand = BEdit.getText().toString();

        TextView ADV1Value = (TextView) view.findViewById(R.id.bandText333333);
        ADV1Value.setText( "Advisory 1" );

        EditText ADV1Edit = (EditText) view.findViewById(R.id.editText333333);
        ADV1Band = ADV1Edit.getText().toString();

        TextView ADV2Value = (TextView) view.findViewById(R.id.bandText444444);
        ADV2Value.setText( "Advisory 2" );

        EditText ADV2Edit = (EditText) view.findViewById(R.id.editText444444);
        ADV2Band = ADV2Edit.getText().toString();

        TextView CValue = (TextView) view.findViewById(R.id.bandText555555);
        CValue.setText( "C Band" );

        EditText CEdit = (EditText) view.findViewById(R.id.editText555555);
        CBand = CEdit.getText().toString();

        TextView FValue = (TextView) view.findViewById(R.id.bandText666666);
        FValue.setText( "F Band" );

        EditText FEdit = (EditText) view.findViewById(R.id.editText666666);
        FBand = FEdit.getText().toString();




        return view;



    }
               public class EditTextWatcher implements TextWatcher {

                   private final TextView target;

                   private EditTextWatcher(TextView target) {
                       this.target = target;
                   }

                   @Override
                   public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                   }

                   @Override
                   public void onTextChanged(CharSequence s, int start, int before, int count) {

                   }

                   @Override
                   public void afterTextChanged(Editable s) {
                       this.target.setText(s);
                   }


                   public void onCreateOptionsMenu(
                         Menu menu, MenuInflater inflater) {
                      inflater.inflate(R.menu.android_apply, menu);
                   }

               }

               public boolean onOptionsItemSelected(android.view.MenuItem item) {
                          // Handle presses on the action bar items
                          switch (item.getItemId()) {
                              case R.id.apply:

                               SharedPreferences sharedPref = getActivity().getSharedPreferences("schedule",Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPref.edit();
                                editor.putString("g_Friday", GBand);
                                editor.putString("b_Friday", BBand);
                                editor.putString("adv1_Friday", ADV1Band);
                                editor.putString("adv2_Friday", ADV2Band);
                                editor.putString("c_Friday", CBand);
                                editor.putString("f_Friday", FBand);
                                editor.commit();



                                  Intent in = new Intent(getActivity(), MainActivity.class);
                                  startActivity(in);

                                  return true;



                          }
                        return true;

               }
        } 

2 个答案:

答案 0 :(得分:4)

仔细看看你在那里做了什么:

  • 当您创建视图时,您正在检索EditText的值。默认情况下,EditText应该为空然后。
  • 然后,您可以获取这些空值并将其存储到以后的 共享首选项。
  • 当您存储值时,实际上并没有从EditText中检索值 - 您只是使用在开头检索的空值。
  • 您的TextWatcher未附加任何内容。

附注:Java编码约定通常调用变量的小写标识符,而大写标识符仅用于类,接口等。对于经验丰富的Java开发人员,您的代码将有些难以理解。

要修复它,请遵循此模式(仅针对GBand显示):

  • 抛弃private String...成员并声明private EditText edtGBand;
  • 位于onCreateView(),包含edtGBand = (EditText) view.findViewById( R.id.editText1111111 );
  • onOptionsItemSelected中,使用editor.putString( "g_Friday", edtGBand.getText().toString() );

答案 1 :(得分:2)

尝试使edittexts受到保护并添加“string”=“edittext”.getText()。toString();在afterTextChanged()

之内