1 import android.app.Activity;
2 import android.content.SharedPreferences;
3 import android.os.Bundle;
4 import android.view.MotionEvent;
5 import android.view.View;
6 import android.view.View.OnTouchListener;
7 import android.widget.EditText;
8 import android.widget.ImageButton;
9
10 public class MySharedPreferencesActivity extends Activity {
11 private EditText user = null;
12 private EditText password = null;
13
14 private ImageButton loginBtn = null;
15 @Override
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.main);
19 user = (EditText)findViewById(R.id.user);
20 password = (EditText)findViewById(R.id.pass);
21 loginBtn = (ImageButton)findViewById(R.id.loginButton);
22 initView();
23 loginBtn.setOnTouchListener(new OnTouchListener(){
24
25 @Override
26 public boolean onTouch(View v, MotionEvent event) {
27 if(event.getAction()==MotionEvent.ACTION_DOWN){
28 v.setBackgroundResource(R.drawable.dengluxitong1);
29 SharedPreferences userInfo = getSharedPreferences("user_info", 0);
30 userInfo.edit().putString("name", user.getText().toString()).commit();
31 userInfo.edit().putString("pass", password.getText().toString()).commit();
32 }
33 else if(event.getAction()==MotionEvent.ACTION_UP){
34 v.setBackgroundResource(R.drawable.dengluxitong);
35 }
36 return false;
37 }
38
39 });
40 }
41 private void initView() {
42 SharedPreferences userInfo = getSharedPreferences("user_info", 0);
43 String username = userInfo.getString("name", "");
44 String pass = userInfo.getString("pass", "");
45 user.setText(username);
46 password.setText(pass);
47 }
48 }