如何在碎片活动中使用共享偏好

时间:2015-06-13 19:44:16

标签: android android-fragments

当我在片段中使用此代码时,我遇到了问题,我的一些帮助做了一些改变。但仍然没有什么错误..

这是我想从正常活动转移到片段活动的代码。

public class MainActivity extends Activity {

final static String SHARED_NAME_STRING="sharedp";
final static String USER_NAME_STRING="user";

Button button;
EditText editText;

SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText=(EditText) findViewById(R.id.userNameEditText);
    button=(Button) findViewById(R.id.enterButton);

    Log.d("DICTIONARY", "main activity started");


    sharedPreferences=getSharedPreferences(SHARED_NAME_STRING, MODE_PRIVATE);
    String userNameString=sharedPreferences.getString(USER_NAME_STRING, "");

    editText.setText(userNameString);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String string=editText.getText().toString();
            Intent intent=new Intent(MainActivity.this, DictionaryListActivity.class);
            intent.putExtra("user", string);

            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString(USER_NAME_STRING, string);
            editor.commit();

            startActivity(intent);

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

它显示错误,如MODE_PRIVATE无法解析为变量。

所以我改为Context.MODE_PRIVATE,然后在" editText =(EditText)findViewById(R.id.userNameEditText);"

的行上显示无法访问的代码

如何改进?

这是我的片段现在看起来像:

public class FragmentA extends Fragment {

final static String SHARED_NAME_STRING="sharedp";
final static String USER_NAME_STRING="user";

Button button;
EditText editText;
Context c;

SharedPreferences sharedPreferences;


public FragmentA() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_a, container, false);
    editText=(EditText) getView().findViewById(R.id.userNameEditText);
    button=(Button) getView().findViewById(R.id.enterButton);

    Log.d("DICTIONARY", "main activity started");

    Context c =getActivity();
    sharedPreferences=this.c.getSharedPreferences(SHARED_NAME_STRING,MODE_PRIVATE);
    String userNameString=sharedPreferences.getString(USER_NAME_STRING, "");

    editText.setText(userNameString);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String string=editText.getText().toString();
            Intent intent=new Intent("android.intent.action.JJJJ");
            intent.putExtra("user", string);

            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString(USER_NAME_STRING, string);
            editor.commit();

            startActivity(intent);

        }
    });
}



}

2 个答案:

答案 0 :(得分:1)

简单地说,替换

getSharedPreferences(SHARED_NAME_STRING,Context.MODE_PRIVATE);

答案 1 :(得分:1)

为什么您在此代码中有return

return inflater.inflate(R.layout.fragment_a, container, false);

建议代码:

// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_a, container, false);
editText= (EditText) view.findViewById(R.id.userNameEditText);
...
return view;
}

注意:

  • 请注意,return关键字已删除。这导致了
  • 的编译错误
  

无法访问的代码......

  • 现在您可以使用getView()
  • ,而不是view
  • 您应该返回View类型对象。因此,方法return view;
  • 末尾的代码onCreateView

祝你好运,玩得开心......