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. [每日100问][2011-8-25]android开发笔记,今天你记住了么
  2. Android实现记住密码功能
  3. Android第十讲笔记(WebView,SharedPreferences)
  4. 做了一个手机上的直播系统
  5. python+appium自动化测试-元素定位之android uiautomatorandroid
  6. Perl登陆Android
  7. Android登录记住密码,AES加密保存密码
  8. 微信开放平台开发第三方授权登陆(三):Android客户端
  9. Android(安卓)WebView 和 手机后退按钮 的故事

随机推荐

  1. JDBC连接的六步实例代码(与mysql连接)
  2. mysql查询优化之100万条数据的一张表优化
  3. MySQL索引知识的一些小妙招总结
  4. MySQL COUNT函数的使用与优化
  5. 解读MySQL的客户端和服务端协议
  6. MySQL 重写查询语句的三种策略
  7. MySQL 可扩展设计的基本原则
  8. 详解MySQL 联合查询优化机制
  9. MySQL Threads_running飙升与慢查询的相
  10. MySQL sql_mode的使用详解