1、视图
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 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <EditText 12 android:id="@+id/et_username" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentTop="true" 17 android:layout_marginLeft="20dp" 18 android:layout_marginTop="22dp" 19 android:ems="10" > 20 21 22 </EditText> 23 24 <EditText 25 android:id="@+id/et_password" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:layout_alignLeft="@+id/et_username" 29 android:layout_below="@+id/et_username" 30 android:layout_marginTop="44dp" 31 android:inputType="textPassword" 32 android:ems="10" /> 33 34 <Button 35 android:onClick="login" 36 android:id="@+id/button1" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:layout_alignBaseline="@+id/cb" 40 android:layout_alignBottom="@+id/cb" 41 android:layout_alignParentRight="true" 42 android:layout_marginRight="32dp" 43 android:text="@string/login" /> 44 45 <TextView 46 android:id="@+id/textView1" 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:layout_alignLeft="@+id/et_username" 50 android:layout_alignParentTop="true" 51 android:text="@string/please_input_username" 52 android:textSize="14sp"/> 53 54 <TextView 55 android:id="@+id/textView2" 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" 58 android:layout_alignLeft="@+id/et_password" 59 android:layout_below="@+id/et_username" 60 android:layout_marginTop="24dp" 61 android:text="@string/please_input_password" 62 android:textSize="14sp" /> 63 64 <CheckBox 65 android:checked="true" 66 android:id="@+id/cb" 67 android:layout_width="wrap_content" 68 android:layout_height="wrap_content" 69 android:layout_alignLeft="@+id/et_password" 70 android:layout_below="@+id/et_password" 71 android:layout_marginTop="27dp" 72 android:text="@string/is_remeber_password" /> 73 74 </RelativeLayout>
2、activity代码
1 package com.example.login; 2 3 import java.util.Map; 4 5 import com.example.login.service.LoginService; 6 7 import android.os.Bundle; 8 import android.app.Activity; 9 import android.text.TextUtils; 10 import android.view.Menu; 11 import android.view.View; 12 import android.widget.CheckBox; 13 import android.widget.EditText; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 18 private EditText et_username; 19 private EditText et_password; 20 private CheckBox cb; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 et_username = (EditText)findViewById(R.id.et_username); 26 et_password = (EditText)findViewById(R.id.et_password); 27 cb = (CheckBox)findViewById(R.id.cb); 28 29 Map<String,String> map = LoginService.getUserInfo(this); 30 if(map != null){ 31 et_username.setText(map.get("username")); 32 et_password.setText(map.get("password")); 33 } 34 } 35 36 public void login(View view) 37 { 38 String username = et_username.getText().toString().trim(); 39 String password = et_password.getText().toString().trim(); 40 if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){ 41 Toast.makeText(this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show(); 42 return; 43 } 44 45 if(cb.isChecked()){ 46 //保存用户信息 47 boolean result = LoginService.savaUserInfo(this,username, password); 48 if(result){ 49 Toast.makeText(this, "保存用户信息成功!", Toast.LENGTH_SHORT).show(); 50 }else{ 51 Toast.makeText(this, "保存用户信息失败!", Toast.LENGTH_SHORT).show(); 52 } 53 } 54 55 if("zhangsan".equals(username) && "123".equals(password)){ 56 Toast.makeText(this, "登陆成功!", Toast.LENGTH_SHORT).show(); 57 }else{ 58 Toast.makeText(this, "用户名或者密码不正确!", Toast.LENGTH_SHORT).show(); 59 } 60 } 61 62 63 }
3、采用普通输入输出流保存数据
1 package com.example.login.service; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.InputStreamReader; 9 import java.util.HashMap; 10 import java.util.Map; 11 12 import android.content.Context; 13 14 public class LoginService { 15 public static boolean savaUserInfo(Context context, String username, String password) 16 { 17 try{ 18 //File file = new File("/data/data/com.example.login/info.txt"); 19 File file = new File(context.getFilesDir(),"info.txt"); 20 FileOutputStream fos = new FileOutputStream(file); 21 fos.write((username + "##" + password).getBytes()); 22 fos.close(); 23 return true; 24 }catch(Exception e){ 25 e.printStackTrace(); 26 return false; 27 } 28 } 29 30 public static Map<String,String> getUserInfo(Context context) 31 { 32 File file = new File(context.getFilesDir(),"info.txt"); 33 try{ 34 FileInputStream fis = new FileInputStream(file); 35 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 36 String str = br.readLine(); 37 String[] infos = str.split("##"); 38 Map<String,String> map = new HashMap<String,String>(); 39 map.put("username", infos[0]); 40 map.put("password", infos[1]); 41 return map; 42 }catch(Exception e){ 43 e.printStackTrace(); 44 return null; 45 } 46 } 47 48 }
时间: 2024-11-07 21:33:37