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;
        }
    
    }

}

 

android 简单文件操作,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。