2012-12-22 96 views
2

我有我的應用程序的登錄屏幕。 (微調,編輯文本,按鈕),我想存儲微調選擇項目和輸入文本共享首選項。在應用程序中隨處使用。String to sharedpreference android

代碼

Button lgnbutton; 
EditText lgnfield; 
Spinner lgnspinner; 

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

lgnbutton = (Button) findViewById(R.id.loginbutton); 
lgnfield = (EditText) findViewById(R.id.loginfield); 
lgnspinner = (Spinner) findViewById(R.id.loginspinner); 





lgnbutton.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     String ID = lgnfield.getText().toString(); 
     String Type = lgnspinner.getSelectedItem().toString(); 


     Intent myIntent = new Intent(LoginActivity.this, MainActivity.class); 
     LoginActivity.this.startActivity(myIntent); 
    } 
}); 
}  
} 

而且這將是很好跳過loginactivity並繼續在MainActivity。

+0

[看這個tuts](http://samir-mangroliya.blogspot.in/p/android-shared-preferences.html) –

+0

你需要保存它變成共享首選項,或者你可以將它作爲一個額外的傳遞? – jcw

+0

我需要每次重新啓動應用程序。 – Georggroenendaal

回答

1

您可以登錄詳細信息及微調所選項目存儲在SharedPreferences

lgnbutton.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     ID = lgnfield.getText().toString(); 
     Type = lgnspinner.getSelectedItem().toString(); 

     SharedPreferences.Editor prefsEditor = loginpreferences.edit(); 
     prefsEditor.putString("ID", ID); 
     prefsEditor.putString("Type", Type); 
     prefsEditor.commit(); 

     Intent myIntent = new Intent(LoginActivity.this, MainActivity.class); 
     LoginActivity.this.startActivity(myIntent); 
    } 
}); 

LoginActivity檢查onCreate如果SharedPreferences存在的登錄信息,如果是,則移動到MainActivity爲:

SharedPreferences loginpreferences; 
String ID="",Type=""; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    loginpreferences = this.getSharedPreferences("loginPrefs", 
              MODE_WORLD_READABLE); 

    ID = loginpreferences.getString("ID", ""); 
    Type = loginpreferences.getString("Type", ""); 

    if(!ID.equalsIgnoreCase("") && !Type.equalsIgnoreCase("")) 
     { 
     //move to MainActivity 
     }else 
    { 
     //stay in LoginActivity 
    } 
+0

謝謝,但我得到一個錯誤loginpreferences.edit(); – Georggroenendaal

+0

@Georggroenendaal:你在哪裏得到錯誤: –

+0

loginpreferences無法解決 – Georggroenendaal

2

調用此上微調點擊

SavePreferences("spinner_item", mySpinner.getSelectedItem().toString()); 

SavePreferences方法的身體。

 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 strSavedMem = sharedPreferences.getString("spinner_item", ""); 
    }