package com.wuyou.login;import java.io.IOException;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.Menu;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import com.wuyou.service.LoginService;public class MainActivity extends Activity {    private CheckBox checkBox;    private EditText usernamEditText;    private EditText passwordeEditText;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        checkBox = (CheckBox) this.findViewById(R.id.cb);        usernamEditText = (EditText) this.findViewById(R.id.username);        passwordeEditText = (EditText) this.findViewById(R.id.password);        // 将之前保存在本地的账号密码取出并设置到文本框中        try {            Map<String, String> map = LoginService.getInfo(this);            usernamEditText.setText(map.get("username"));            passwordeEditText.setText(map.get("password"));        } catch (Exception e) {            Log.i("main", "取不出账号密码");        }    }    @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;    }    /**     * 点击“登陆”按钮触发的时间     *      * @param v     */    public void login(View v) {        Log.i("login", "在登陆中");        String username = usernamEditText.getText().toString().trim();        String password = passwordeEditText.getText().toString().trim();        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {            Toast.makeText(this, "用户名和账号不能为空!", Toast.LENGTH_SHORT).show();        } else {            // 判断是否选择了记住密码            if (checkBox.isChecked()) {                try {                    LoginService.saveInfo(this, username, password);                    Toast.makeText(this, "记住密码成功!", Toast.LENGTH_SHORT).show();                } catch (IOException e) {                    Toast.makeText(this, "无法记住密码", Toast.LENGTH_SHORT).show();                }            }            if ("zhangsan".equals(username) && "123".equals(password)) {                Toast.makeText(this, "登陆成功!", Toast.LENGTH_SHORT).show();            } else {                Toast.makeText(this, "账号密码有误!", Toast.LENGTH_SHORT).show();            }        }    }}
<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入账号" />    <EditText        android:id="@+id/username"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入密码" />    <EditText        android:id="@+id/password"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <CheckBox            android:id="@+id/cb"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="记住密码" />        <!-- 记住login的方法有一个参数View -->        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:onClick="login"            android:text="登陆" />    </RelativeLayout></LinearLayout>
package com.wuyou.service;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import android.content.Context;public class LoginService {    public static void saveInfo(Context context, String username,            String password) throws IOException {        File file = new File(context.getFilesDir(), "info.txt");        FileWriter fileWriter = new FileWriter(file);        fileWriter.write(username + "##" + password);        fileWriter.close();    }    public static Map<String, String> getInfo(Context context)            throws IOException {
     //这里只做简单的保存,如果要保存配置文件信息,可以使用SharedPreference类保存,请关注本类别的该文章 File file
= new File(context.getFilesDir(),"info.txt"); Map<String, String> map = new HashMap<String, String>(); BufferedReader bf = new BufferedReader(new InputStreamReader( new FileInputStream(file))); String info = bf.readLine(); String username = ""; String password = ""; if (info != null) { String[] infos = info.split("##"); username = infos[0]; password = infos[1]; map.put("username", username); map.put("password", password); } bf.close(); return map; }}

更多相关文章

  1. Android实现动态显示或隐藏密码输入框的内容
  2. Android 记住密码功能
  3. Android : 登录并保存密码
  4. Android实现记住密码功能
  5. Android EditText 为空提示 密码隐藏
  6. Android EditText密码框中字体和普通的输入框不同
  7. Android登录记住密码,AES加密保存密码
  8. Android逆向之旅---Android中锁屏密码算法解析以及破解方案

随机推荐

  1. SQL Server并发处理存在就更新解决方案探
  2. Sqlserver事务备份和还原的实例代码(必看
  3. SQLServer存储过程创建和修改的实现代码
  4. Sql Server临时表和游标的使用小结
  5. SQL查询方法精华集
  6. SQLServer中使用扩展事件获取Session级别
  7. Sql Server数据库各版本功能对比
  8. SQL Server 常用函数使用方法小结
  9. SQL Server中关于基数估计计算预估行数的
  10. SQL Server 公用表表达式(CTE)实现递归的