在编写安卓登录界面时,我们如何记住密码,方便下一次登录呢?
首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"记住密码"
然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.csdn.www.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" /> <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="密码" /> <EditText android:id="@+id/userpass" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:password="true" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="73dp" android:text="登陆" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:checked="true" android:text="记住密码" /> </RelativeLayout> </LinearLayout>
视图效果:
在src下的java文件里创建一个SaveFile.java类,旨在记住密码之后如何保存用户名、密码和获取保存数据
package com.csdn.www; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import android.content.Context; public class SaveFile { public static boolean save(Context context,String name,String pass){ try { //File f=new File("/data/data/com/csdn/www/info.txt"); File f=new File(context.getFilesDir(),"info.txt"); //context.getFilesDir();//返回一个目录 /data/data/包名/files FileOutputStream fos=new FileOutputStream(f); fos.write((name+"=="+pass).getBytes()); fos.close(); return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } /** * 获取保存的数据 * @param context * @return */ public static Map<String,String> getSaveFiles(Context context){ File f=new File(context.getFilesDir(),"info.txt"); try { FileInputStream fis=new FileInputStream(f); BufferedReader br=new BufferedReader(new InputStreamReader(fis)); String str=br.readLine(); String[] infos=str.split("=="); Map<String,String> map=new HashMap<String,String>(); map.put("username", infos[0]); map.put("userpass", infos[1]); return map; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
最后在src下的java文件里MainActivity.java
package com.csdn.www; import java.util.Map; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.text.TextUtils; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private static final String TAG = "MainActivity"; private EditText username, userpass; private CheckBox checkBox1; private Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); username = (EditText) this.findViewById(R.id.username); userpass = (EditText) this.findViewById(R.id.userpass); checkBox1 = (CheckBox) this.findViewById(R.id.checkBox1); Map<String, String> map=SaveFile.getSaveFiles(this); if(map!=null){ username.setText(map.get("username")); userpass.setText(map.get("userpass")); } login = (Button) findViewById(R.id.login); login.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String un = username.getText().toString().trim(); String up = userpass.getText().toString().trim(); if (TextUtils.isEmpty(un) || TextUtils.isEmpty(up)) { Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show(); } else { // 登陆 //是否保存密码 if (checkBox1.isChecked()) { // 保存用户名和密码 Log.i(TAG, "需要保存用户名密码"); boolean flag=SaveFile.save(MainActivity.this,un, up); if(flag){ Toast.makeText(MainActivity.this, "信息保存成功", 0).show(); } } // 登陆发送消息到服务器,服务器验证是否正确 if ("zhangsan".equals(un) && "123".equals(up)) { Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "用户名或密码错误", 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; } }
会显示“信息保存成功”,“用户名或密码错误”
在Eclipse中的”File Explorer“里,目录”data/data/com/csdn/www/info.txt“,用户名和密码会保存在info.txt里