【问题标题】:How can I make my android app have SharedPreferences as just one set of preferences, so that I can control it and clear the preferences with a button如何让我的 android 应用程序将 SharedPreferences 作为一组首选项,以便我可以控制它并使用按钮清除首选项
【发布时间】:2013-04-05 12:01:41
【问题描述】:

我试图拥有一次 SharedPreferences 并将其控制为不同的方法,而不是在不同的方法中使用不同的 SharedPreferences。我在 onCreate、LoadPreferences、SavePreferences 和 ClearTextViews 方法中有 SharedPreferences。我想这样做,以便我将首选项从 EditText 中输入的文本保存到 TextViews,然后能够使用按钮将它们全部清除。如果可以,请你帮助我。以下是相关代码:

公共类 notesActivity 扩展 Activity 实现 OnClickListener {

Button saveNote;
Button clearText;
EditText note;
TextView textSavedNote1, textSavedNote2, textSavedNote3, textSavedNote4, textSavedNote5, textSavedNote6;
SharedPreferences spNote;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notes);

    saveNote = (Button)this.findViewById(R.id.saveNotes);
    saveNote.setOnClickListener(this);

    clearText = (Button)this.findViewById(R.id.clearAllText);
    clearText.setOnClickListener(this);

    textSavedNote1 = (TextView)findViewById(R.id.stringSavedNote1);
    textSavedNote2 = (TextView)findViewById(R.id.stringSavedNote2);
    textSavedNote3 = (TextView)findViewById(R.id.stringSavedNote3);
    textSavedNote4 = (TextView)findViewById(R.id.stringSavedNote4);
    textSavedNote5 = (TextView)findViewById(R.id.stringSavedNote5);
    textSavedNote6 = (TextView)findViewById(R.id.stringSavedNote6);

    note = (EditText)this.findViewById(R.id.notes);

    spNote = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor edit = spNote.edit();
    edit.putString("note"+saveNote,note.getText().toString());
    edit.commit();
}



public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Goes back to the home screen
    case R.id.item1:  Intent i = new Intent(notesActivity.this, UserSettingActivity.class);
    startActivity(i);

    case R.id.item2:
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
    // Refreshes the page.   
    case R.id.item3:
            finish();
            startActivity(getIntent());
        default:
            return super.onOptionsItemSelected(item);
    }
}


public void onClick (View v){
    if(v==saveNote){
        SavePreferences("NOTE1", note.getText().toString());
        LoadPreferences();
        note.setText("");
        if(textSavedNote1.getText().toString().length()>0){
            SavePreferences("NOTE2", note.getText().toString());
            LoadPreferences();
            note.setText("");
        }
        else{
    }
}
    else if(v==clearText){
        ClearTextViews();
    }
}

private void SavePreferences(String key, String value){
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
   }

private void LoadPreferences(){
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    String strSavedNote1 = sharedPreferences.getString("NOTE1", "");
    String strSavedNote2 = sharedPreferences.getString("NOTE2", "");
    String strSavedNote3 = sharedPreferences.getString("NOTE3", "");
    String strSavedNote4 = sharedPreferences.getString("NOTE4", "");
    String strSavedNote5 = sharedPreferences.getString("NOTE5", "");
    String strSavedNote6 = sharedPreferences.getString("NOTE6", "");
    textSavedNote1.setText(strSavedNote1);
    textSavedNote2.setText(strSavedNote2);
    textSavedNote3.setText(strSavedNote3);
    textSavedNote4.setText(strSavedNote4);
    textSavedNote5.setText(strSavedNote5);
    textSavedNote6.setText(strSavedNote6);
   }

private void ClearTextViews(){ 

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.commit();

}

}

【问题讨论】:

  • 别忘了把break;放在switch () {case: *break*;}的末尾。否则,您的代码将同时运行两种情况:R.id.item1R.id.item2。此外,您不能将switch-caseR.id.* 一起使用,因为R.id.* 不再是常量值(因为来自一些ADT 更新),因此您应该在这里使用if-else

标签: java android sharedpreferences preferences


【解决方案1】:
【解决方案2】:

试试下面的代码。

/*I have added one more button, on click on that button i have set call clearText function and set all text     empty, each time before clicking on save button you click first clear button then next value will be inserted in next textview.*/
public class SharedPreferenceJustOneSetOfPreferencesActivity extends Activity implements OnClickListener{

    private Button saveNote,clearText,ClearAll;
    static TextView textSavedNote1, textSavedNote2, textSavedNote3, textSavedNote4, textSavedNote5, textSavedNote6;
    private EditText note;
    private SharedPreferences spNote;
    private static final String TAG = SharedPreferenceJustOneSetOfPreferencesActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        saveInPreference();
    }

    private void initView() {
        textSavedNote1 = (TextView)findViewById(R.id.textSavedNote1);
        textSavedNote2 = (TextView)findViewById(R.id.textSavedNote2);
        textSavedNote3 = (TextView)findViewById(R.id.textSavedNote3);
        textSavedNote4 = (TextView)findViewById(R.id.textSavedNote4);
        textSavedNote5 = (TextView)findViewById(R.id.textSavedNote5);
        textSavedNote6 = (TextView)findViewById(R.id.textSavedNote6);
        note = (EditText)findViewById(R.id.note);
        saveNote = (Button)findViewById(R.id.saveNote);
        clearText = (Button)findViewById(R.id.clearText);
        ClearAll = (Button)findViewById(R.id.ClearAll);
        saveNote.setOnClickListener(this);
        clearText.setOnClickListener(this); 
        ClearAll.setOnClickListener(this);
    }
    private void saveInPreference() {
        spNote = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor edit = spNote.edit();
        edit.putString("note"+saveNote,note.getText().toString());
        edit.commit();
    }
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;        
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
        case R.id.item1:  Intent i = new Intent(SharedPreferenceJustOneSetOfPreferencesActivity.this, UserSettingActivity.class);
        startActivity(i);
        case R.id.item2:
                Intent intent = new Intent(SharedPreferenceJustOneSetOfPreferencesActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
        case R.id.item3:
              finish();
              startActivity(getIntent());
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    @Override
    public void onClick(View view) {
        if (view == saveNote) {
            String textvedNote1 =textSavedNote1.getText().toString();
            String textvedNote2= textSavedNote2.getText().toString();
            String textvedNote3 =textSavedNote3.getText().toString();
            String textvedNote4= textSavedNote4.getText().toString();
            String textvedNote5 =textSavedNote5.getText().toString();
            String textvedNote6= textSavedNote6.getText().toString();

            if (textvedNote1.equals("")) {          
                SavePreferences("NOTE1", note.getText().toString());
                Log.e(TAG,"textvedNote1: Inside "+textvedNote1.length());
                LoadPreferences();
                note.setText("");
            }else  if (textvedNote2.equals("")&& !textvedNote1.equals("")) {            
                SavePreferences("NOTE2", note.getText().toString());
                Log.e(TAG,"textvedNote2: Inside "+textvedNote1.length());
                LoadPreferences();
                note.setText("");
            } else if (textvedNote3.equals("")&& !textvedNote2.equals("")) {
                Log.e(TAG,"textvedNote3: Inside "+textvedNote2.length());
                SavePreferences("NOTE3", note.getText().toString());
                LoadPreferences();
                note.setText("");
            } else if (textvedNote4.equals("")&& !textvedNote3.equals("")) {
                Log.e(TAG,"textvedNote4: Inside "+textvedNote3.length());
                SavePreferences("NOTE4", note.getText().toString());
                LoadPreferences();
                note.setText("");
            } else if (textvedNote5.equals("")&& !textvedNote4.equals("")) {
                Log.e(TAG,"textvedNote5: Inside "+textvedNote4.length());
                SavePreferences("NOTE5", note.getText().toString());
                LoadPreferences();
                note.setText("");
            } else if (textvedNote6.equals("")&& !textvedNote5.equals("")) {
                SavePreferences("NOTE6", note.getText().toString());
                Log.e(TAG,"textvedNote6: Inside "+textvedNote5.length());
                LoadPreferences();
                note.setText("");
            }
        } else if (view == clearText) {
            ClearTextViews();
        }else if (view == ClearAll){
            ClearTextViews();
            textSavedNote1.setText("");
            textSavedNote2.setText("");
            textSavedNote3.setText("");         
            textSavedNote4.setText("");         
            textSavedNote5.setText("");
            textSavedNote6.setText("");
        }

    }
    private void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }
    private void LoadPreferences(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String strSavedNote1 = sharedPreferences.getString("NOTE1", "");
        String strSavedNote2 = sharedPreferences.getString("NOTE2", "");
        String strSavedNote3 = sharedPreferences.getString("NOTE3", "");
        String strSavedNote4 = sharedPreferences.getString("NOTE4", "");
        String strSavedNote5 = sharedPreferences.getString("NOTE5", "");
        String strSavedNote6 = sharedPreferences.getString("NOTE6", "");
        if (!strSavedNote1.equals("")) {
            Log.e(TAG,"LoadPreferences1: "+strSavedNote1);
            textSavedNote1.setText(strSavedNote1);
        } else if (!strSavedNote2.equals("")) {
            Log.e(TAG,"LoadPreferences2:  "+strSavedNote2);
            textSavedNote2.setText(strSavedNote2);
        } else if (!strSavedNote3.equals("")) {
            Log.e(TAG,"LoadPreferences3:  "+strSavedNote3);
            textSavedNote3.setText(strSavedNote3);
        } else if (!strSavedNote4.equals("")) {
            textSavedNote4.setText(strSavedNote4);
        } else if (!strSavedNote5.equals("")) {
            textSavedNote5.setText(strSavedNote5);
        } else if (!strSavedNote6.equals("")) {
            textSavedNote6.setText(strSavedNote6);
        }
    }
    private void ClearTextViews(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
}

【讨论】:

    猜你喜欢
    • 2013-01-16
    • 2016-08-14
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多