Android :内部文件中写操作

在内部文件存储的写操作中与java中文件读写类似,都需要用到文件的输入输出流。

界面设计

这里写图片描述

布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#99ccff" >
    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"       
        android:hint="请输入用户名"
        android:id="@+id/et_username"   
        />
    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"       
        android:hint="请输入密码"  
        android:password="true"
        android:id="@+id/et_password" 
        />
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <CheckBox 
            android:id="@+id/cb"
          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 
       android:layout_gravity="bottom"
       android:layout_width="match_parent"
       android:layout_height="match_parent" 
      >
    <ImageView 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_gravity="bottom"
    android:src="@drawable/nv"
    />
  </LinearLayout>  

</LinearLayout>

main Activity代码

package com.example.instorage;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.instorage);
    }
   public void login (View v){
       EditText et_username=(EditText) findViewById(R.id.et_username);
       EditText et_password=(EditText) findViewById(R.id.et_password);
       String name=et_username.getText().toString();
       String password=et_password.getText().toString();       


       CheckBox cb=(CheckBox) findViewById(R.id.cb);
       //判断选框有没有被勾选,调用isChecked()
       if(cb.isChecked()){
           //这就是内部存储空间的路径
           //先进行部署,之后通过 Window-show View-other-File Explorer-data/data下找到相应包名的文件
              File file=new File("data/data/com.example.instorage/info.txt");
              FileOutputStream os;
            try {
                os = new FileOutputStream(file);
                String buffer=name+"##"+password;
                os.write(buffer.getBytes());
                os.close();//记得关闭文件               
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }             
           }
           Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();

   }


}

学习注意点

1、在布局文件中的几种布局方式的运用
2、布局文件中check Box的使用方法
3、Button的onClick属性直接使用时需要在main activity中写入此方法
4、main activity中对check box勾选框的检测,isChecked()
5、main activity中文件的输入输出流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值