往内部存储写文件

来源:互联网 发布:农商银行柜员工资知乎 编辑:程序博客网 时间:2024/04/30 02:08

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

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

这里写图片描述

0 0
原创粉丝点击