2013-03-06 56 views
1

我正在開發一個android應用程序,用於使用消息獲取數據。密碼創建這裏是我的代碼,我如何使用sharedPreference類來存儲密碼?如何使用sharedPreference類存儲密碼?

 public class MainActivity extends Activity { 

Button Switchon; 
EditText passwd; //button name 
String ms; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    passwd = (EditText) findViewById(R.id.passwd); 
    Switchon = (Button) findViewById(R.id.Switchon); 



    Switchon.setOnClickListener(new View.OnClickListener() { 



     @Override 
     public void onClick(View arg0) { 
     @SuppressWarnings("unused") 
     String ms = passwd.getText().toString(); 
      Toast.makeText(getApplicationContext(), "You have successfully created and this app is on", Toast.LENGTH_SHORT).show(); 

     } 
    }); 

} 

@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; 
} 



    } 
+1

所以,什麼你已經嘗試在SharePrefrence的代碼? – 2013-03-06 10:41:45

+0

@akbari dipali是的,當按鈕點擊時存儲密碼..請幫助我....感謝 – Abhilash 2013-03-08 05:59:56

回答

1

存儲值在共享偏好:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString("password","123456"); 
    editor.commit(); 

要從共享偏好檢索值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    String name = preferences.getString("password",""); 
+1

添加到Nirav的答案,不要忘記哈希密碼。永遠不要將密碼保存爲純文本。請參閱此鏈接以獲取如何對其進行哈希處理的示例。 http://www.mkyong.com/java/java-sha-hashing-example/ – greenkode 2013-03-06 13:42:15

+0

@Umoh:我知道,這只是個例子 – 2013-03-06 14:00:10