SharedPreferences保存属性(其背后还是XML文件的存储)

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class XMLfileActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button=(Button)this.findViewById(R.id.buttonsave);
Button buttonback=(Button)this.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText text1=(EditText)XMLfileActivity.this.findViewById(R.id.username);
EditText text2=(EditText)XMLfileActivity.this.findViewById(R.id.pwd);
SharedPreferences sharepreference=getSharedPreferences("us",Context.MODE_PRIVATE);
Editor editor=sharepreference.edit();
editor.putString("usname", text1.getText().toString());
editor.putString("uspassword", text2.getText().toString());
editor.commit();
text1.setText("");
text2.setText("");
}
});
buttonback.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText text1=(EditText)XMLfileActivity.this.findViewById(R.id.username);
EditText text2=(EditText)XMLfileActivity.this.findViewById(R.id.pwd);
SharedPreferences sharepreference=getSharedPreferences("us",Context.MODE_PRIVATE);
String name=sharepreference.getString("usname", "");
String psd=sharepreference.getString("uspassword", "");
text1.setText(name);
text2.setText(psd);


}
});
}
}

 


posted @ 2011-12-08 17:06  乌托邦.  阅读(1580)  评论(0编辑  收藏  举报