如何使用sharedPreference类存储密码?

时间:2013-03-06 10:40:14

标签: java android eclipse

我正在开发一个用于使用消息获取数据的Android应用程序。密码创建在这里是我的代码,如何使用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 个答案:

答案 0 :(得分: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","");