首页 > 代码库 > 使用SharedPreferences存储用户名信息
使用SharedPreferences存储用户名信息
1 package com.example.sharedpreferences; 2 3 import android.os.Bundle; 4 import android.preference.PreferenceManager; 5 import android.app.Activity; 6 import android.content.SharedPreferences; 7 import android.content.SharedPreferences.Editor; 8 import android.view.Menu; 9 import android.view.View;10 import android.view.View.OnClickListener;11 import android.widget.Button;12 import android.widget.CheckBox;13 import android.widget.EditText;14 import android.widget.TextView;15 import android.widget.Toast;16 17 public class MainActivity extends Activity implements OnClickListener{18 private EditText et1,et2;19 private CheckBox cb1;20 private Button bt1,bt2;21 private SharedPreferences pref;22 private Editor editor;23 private TextView tv1;24 @Override25 protected void onCreate(Bundle savedInstanceState) {26 super.onCreate(savedInstanceState);27 setContentView(R.layout.activity_main);28 // SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);29 // SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);30 // Editor editor = pref.edit();31 // editor.putString("name", "阿狸");32 // editor.putLong("time", System.currentTimeMillis());33 // editor.commit();34 // System.out.println(pref.getString("name", ""));35 // System.out.println(pref.getLong("time", 0));36 37 et1 = (EditText) findViewById(R.id.editText1);38 et2 = (EditText) findViewById(R.id.editText2);39 cb1 = (CheckBox) findViewById(R.id.checkBox1);40 bt1 = (Button) findViewById(R.id.button1);41 bt2 = (Button) findViewById(R.id.button2);42 tv1 = (TextView) findViewById(R.id.textView1);43 bt1.setOnClickListener(this);44 bt2.setOnClickListener(this);45 pref = getSharedPreferences("mypref1", MODE_PRIVATE);46 editor = pref.edit();47 String name = pref.getString("username", "");48 if(name == null) {49 cb1.setChecked(false);50 }else{51 cb1.setChecked(true);52 et1.setText(name);53 }54 }55 56 @Override57 public void onClick(View v) {58 // TODO Auto-generated method stub59 switch (v.getId()) {60 case R.id.button1:61 String name = et1.getText().toString().trim();62 String pass = et2.getText().toString().trim();63 if(name.equals("admin")&&pass.equals("123456")){64 if(cb1.isChecked()){65 editor.putString("username", "admin");66 editor.commit();67 }else {68 editor.remove("username");69 editor.commit();70 }71 Toast.makeText(MainActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();72 }else{73 Toast.makeText(MainActivity.this, "登陆失败!", Toast.LENGTH_SHORT).show();74 }75 break;76 case R.id.button2:77 78 break;79 }80 }81 82 }
上面是MainActivity的代码,下面是UI的代码。
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 7 <TextView 8 android:id="@+id/textView1" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:layout_alignParentLeft="true"12 android:layout_alignParentTop="true"13 android:layout_marginLeft="17dp"14 android:layout_marginTop="18dp"15 android:text="用户名:" />16 17 <EditText18 android:id="@+id/editText1"19 android:layout_width="wrap_content"20 android:layout_height="wrap_content"21 android:layout_alignBaseline="@+id/textView1"22 android:layout_alignBottom="@+id/textView1"23 android:layout_alignRight="@+id/button2"24 android:layout_toRightOf="@+id/textView1"25 android:ems="10" >26 27 <requestFocus />28 </EditText>29 30 <TextView31 android:id="@+id/textView2"32 android:layout_width="wrap_content"33 android:layout_height="wrap_content"34 android:layout_alignLeft="@+id/textView1"35 android:layout_below="@+id/editText1"36 android:layout_marginTop="14dp"37 android:text="密码:" />38 39 <EditText40 android:id="@+id/editText2"41 android:layout_width="wrap_content"42 android:layout_height="wrap_content"43 android:layout_alignLeft="@+id/editText1"44 android:layout_alignRight="@+id/editText1"45 android:layout_below="@+id/editText1"46 android:ems="10"47 android:inputType="textPassword" />48 49 <CheckBox50 android:id="@+id/checkBox1"51 android:layout_width="wrap_content"52 android:layout_height="wrap_content"53 android:layout_alignLeft="@+id/textView2"54 android:layout_below="@+id/editText2"55 android:layout_marginTop="16dp"56 android:checked="false"57 android:text="保存" />58 59 <Button60 android:id="@+id/button1"61 android:layout_width="wrap_content"62 android:layout_height="wrap_content"63 android:layout_alignLeft="@+id/checkBox1"64 android:layout_below="@+id/checkBox1"65 android:layout_marginTop="14dp"66 android:text="确定" />67 68 <Button69 android:id="@+id/button2"70 android:layout_width="wrap_content"71 android:layout_height="wrap_content"72 android:layout_alignBaseline="@+id/button1"73 android:layout_alignBottom="@+id/button1"74 android:layout_alignParentRight="true"75 android:layout_marginRight="26dp"76 android:text="取消" />77 78 </RelativeLayout>
慕课网是个好网站(*^__^*)
使用SharedPreferences存储用户名信息
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。