android 简单文件操作

1.布局

复制代码
<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="com.example.logindemo.MainActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="请输入用户名" />
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_username"
        />
     <TextView
        
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="请输入密码" />
    <EditText 
        android:inputType="textPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_password"
        />
    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        
        >
        <CheckBox 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cb_remember"
            android:text="记住密码"
            android:checked="true"
            />
        <Button
            android:onClick="login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="登录"
            android:id="@+id/btn_login" />
    </RelativeLayout>

</LinearLayout>
复制代码

2.MainActivity.java

复制代码
package com.example.logindemo;

import java.util.Map;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.logindemo.service.LoginService;


public class MainActivity extends ActionBarActivity {
    
    private EditText et_username;
    private EditText et_pwd;
    private CheckBox cb_remember;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_username = (EditText)this.findViewById(R.id.et_username);
        et_pwd = (EditText)this.findViewById(R.id.et_password);
        cb_remember = (CheckBox)this.findViewById(R.id.cb_remember);
        
        Map<String,String> map = LoginService.getUserInfo(this);
        if(map!=null)
        {
            et_username.setText(map.get("username"));
            et_pwd.setText(map.get("password"));
        }
    }

    
    public void login(View v){
        
        String username = et_username.getText().toString();
        String pwd = et_pwd.getText().toString();
        
        if(TextUtils.isEmpty(username) || TextUtils.isEmpty(pwd)){
            
            Toast.makeText(MainActivity.this, "username or password is empty", Toast.LENGTH_SHORT).show();
            return;
        }
        else
        {
            
            if(cb_remember.isChecked())
            {
                boolean result = LoginService.saveUserInfo(this, username, pwd);
                if(result)
                {
                    Toast.makeText(MainActivity.this, "save user info success", Toast.LENGTH_SHORT).show();
                }
            }
            
            if("test".equals(username) && "123".equals(pwd))
            {
                Toast.makeText(MainActivity.this, "login success", Toast.LENGTH_SHORT).show();
            }
            else
            {
                
                Toast.makeText(MainActivity.this, "username or password is error", Toast.LENGTH_SHORT).show();
            }
        }
        
    }
}
复制代码

3.service

复制代码
package com.example.logindemo.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;

public class LoginService {
    
    public static boolean saveUserInfo(Context context,String username,String pwd)
    {
        try
        {
            File file = new File(context.getFilesDir(),"info.txt");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write((username+"##"+pwd).getBytes());
            fos.close();
            return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return false;
        }
        
    }
    
    public static Map<String,String> getUserInfo(Context context)
    {
        try
        {
            File file = new File(context.getFilesDir(),"info.txt");
            FileInputStream fis = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String strs = br.readLine();
            String arr[] = strs.split("##");
            Map<String,String> map = new HashMap<String,String>();
            map.put("username", arr[0]);
            map.put("password", arr[1]);
            return map;
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    
    }

}
复制代码

 

posted @   大空白纸  阅读(257)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示