往内部存储写文件

本文介绍了一个简单的安卓应用登录功能实现方法,通过XML定义界面布局,包含用户名与密码输入框及记住密码选项,并使用Java代码将用户登录信息写入本地文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用案列演示。用户登录,把帐号、密码数据写入文件。

1.构建界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.dev.readwritefile.MainActivity">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"/>

    <EditText
        android:id="@+id/et_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="请输入密码"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/Rememb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
            android:layout_centerVertical="true"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            android:layout_alignParentRight="true"
            android:onClick="login"/>
    </RelativeLayout>

</LinearLayout>

这里写图片描述

2.编写处理业务逻辑代码

package com.example.dev.readwritefile;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * 登录操作
     * @param v View对象
     */
    public void login(View v){
        //获取输入的用户名和密码
        EditText et_name = (EditText)findViewById(R.id.et_name);
        EditText et_pwd = (EditText)findViewById(R.id.et_pwd);
        String name = et_name.getText().toString();
        String pwd = et_pwd.getText().toString();

        //判断有无勾选『记住密码』
        CheckBox box = (CheckBox)findViewById(R.id.Rememb);
        if (box.isChecked()){
            //如果勾选,就把帐号和密码写入文件
            File f = new File("data/data/com.example.dev.readwritefile/info.txt");
            /*
            * data/data/com.example.dev.readwritefile
            * com.example.dev.readwritefile是此项目包名
            * */

            FileOutputStream fos;
            try {
                fos = new FileOutputStream(f);
                fos.write((name+":"+pwd).getBytes());
                fos.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        //Toast对话框提示
        Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值