Android中的SharedPreferences有效

时间:2016-07-11 15:25:36

标签: android android-sharedpreferences

我创建了一个sharedprefreference对象并尝试在同一个活动中检索我的数据,这是一个允许的操作吗?问题是,一旦应用程序开始运行,并且我在textview中输入了文本,输入的文本就不会再出现在单独的文本视图中。应用程序假设接收用户输入的文本并向下显示几个dp,但输入的文本从未再次出现。非常感谢你们所有人。

    final static String My_PREFERENCES= "sharedPreferences";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    foodTV = (TextView) findViewById(R.id.foodTV);
    foodET = (EditText) findViewById(R.id.foodET);
    display = (TextView) findViewById(R.id.displayText);

    SharedPreferences sharedPreferences =
            getSharedPreferences(My_PREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor edit = sharedPreferences.edit();
    edit.clear();
    edit.putString("foodname", foodET.getText().toString());
    edit.commit();

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(My_PREFERENCES, Context.MODE_PRIVATE);
            String food = sharedPreferences.getString("foodname", "");
            display.setText(food);
        }
    });
}

2 个答案:

答案 0 :(得分:0)

将您的代码更改为

foodTV = (TextView) findViewById(R.id.foodTV);
foodET = (EditText) findViewById(R.id.foodET);
display = (TextView) findViewById(R.id.displayText);

  foodET.setOnEditorActionListener(
              new EditText.OnEditorActionListener() {
              @Override
              public boolean onEditorAction(TextView v, int actionId,KeyEvent event) {
                     if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                           actionId == EditorInfo.IME_ACTION_DONE ||
                         event.getAction() == KeyEvent.ACTION_DOWN &&
                         event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
    if (!event.isShiftPressed()) {
      SharedPreferences sharedPreferences =
        getSharedPreferences(My_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.clear();
        edit.putString("foodname", foodET.getText().toString());
        edit.commit();
       return true; 
      }                
       }
        return false; 
     }
      });

 button = (Button) findViewById(R.id.button);
 button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
             SharedPreferences sharedPreferences =    getApplicationContext().getSharedPreferences(My_PREFERENCES, Context.MODE_PRIVATE);
         String food = sharedPreferences.getString("foodname", "");
        display.setText(food);
    }
});

答案 1 :(得分:0)

In fact, I would suggest you using an amazing https://github.com/pilgr/Paper

See how's simple is that:

Only two things are needed:

  1. Initialize library inside your app (or even Activity itself) class
  2. Make calls!

MyApp.java

Just initialize Paper

Paper.init(getApplicationContext());

MyFragment.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Sample food name
    String foodName = "apple";

    // Write it to paper
    Paper.book().write("foodname", foodName);

    // Then restore what we have just saved!
    String restoredFoodName = Paper.book().read("foodname");
}

Please do note that those calls are not asynchronous! But you can write few simple AsyncTask-s to make them be so.