1

我正在开发一个用于使用消息获取数据的 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;
}



    }
4

1 回答 1

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","");
于 2013-03-06T10:46:30.237 回答