Android studio中如何将用户输入的数据存在本地?以及R变红该如何处理

我们小组在Android应用程序开发的过程中,因为大赛在即,会遇到来不及构建服务器,无法构建数据库。所以只能将数据暂时存储在用户本地,于是就此问题进行了研究。

首先,提出解决R文件变红的解决方案。在Android studio这个软件中,会遇到各种使用时出现的问题。有时候工程建出来,打开mainactivity,里面的R就是红色的。这个时候,我就尝试了以下几种办法:

1.    选择File-->Project Structure-->在左边选择出错的项目名-->修改 Build Tools Version--->再改回来--->OK  

2.在出现找不到R文件时,有很多情况都是资源文件引入不正确导致的问题,比如图片、String等资源。

而在Android Studio中可以很快定位问题。


如上图,选中Build–>Rebuild Project 之后,编译器自动就会提示你出错的地方,如下:

Error:(47, 21) No resource found that matches the given name (at 'src' with value '@drawable/a_left').
Error:(48, 36) No resource found that matches the given name (at 'contentDescription' with value '@string/start_a').
  • 1
  • 2

以上的问题就是图片和string资源找不到才报错的。根据提示找到对应的资源文件修改就可以了。

3.    如果你查看了没有任何问题,R文件依旧变红,那么你可以试试看Build-->Clean Project。我就是这样修改好的。

4.    选择 Tools-->Android-->Sync Project With Gradle Files

接下来要为大家介绍,如何在本地存储数据。先写好activity中的XML,我采用的是相对布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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.asus.location.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:id="@+id/textView5"
        android:text="数据采集"
        android:textSize="25dp"
        android:textColor="#000000"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        />
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#cccccc"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="00dp" />

    <TextView
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:text="Ymin:"
        android:textSize="20dp"
        android:id="@+id/textView4"
        android:textColor="#000000"
        android:layout_marginTop="450dp"
        android:layout_marginLeft="20dp" />
    <EditText
        android:layout_width="160dp"
        android:layout_height="40dp"
        android:id="@+id/text1"
        android:textSize="20dp"
        android:textColor="#000000"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="220dp"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:text="房间名:"
        android:textSize="20dp"
        android:id="@+id/textView1"
        android:textColor="#000000"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="40dp" />
    <EditText
        android:layout_width="160dp"
        android:layout_height="40dp"
        android:id="@+id/name"
        android:textSize="20dp"
        android:textColor="#000000"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="40dp"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:text="房间号:"
        android:textSize="20dp"
        android:id="@+id/textView2"
        android:textColor="#000000"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="100dp" />
    <EditText
        android:layout_width="160dp"
        android:layout_height="40dp"
        android:id="@+id/id"
        android:textSize="20dp"
        android:textColor="#000000"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="100dp"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:text="楼层:"
        android:textColor="#000000"
        android:textSize="20dp"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="160dp" />
    <EditText
        android:layout_width="160dp"
        android:layout_height="40dp"
        android:id="@+id/floor"
        android:textSize="20dp"
        android:textColor="#000000"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="160dp"/>

    <TextView
    android:layout_width="80dp"
    android:layout_height="30dp"
    android:text="Xmax:"
    android:textSize="20dp"
    android:id="@+id/textView6"
    android:textColor="#000000"
    android:layout_marginTop="270dp"
    android:layout_marginLeft="20dp" />
    <EditText
        android:layout_width="160dp"
        android:layout_height="40dp"
        android:id="@+id/text4"
        android:textSize="20dp"
        android:textColor="#000000"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="400dp"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:text="Xmin:"
        android:textSize="20dp"
        android:id="@+id/textView7"
        android:textColor="#000000"
        android:layout_marginTop="330dp"
        android:layout_marginLeft="20dp" />
    <EditText
        android:layout_width="160dp"
        android:layout_height="40dp"
        android:id="@+id/text2"
        android:textSize="20dp"
        android:textColor="#000000"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="280dp"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:text="Ymax:"
        android:textSize="20dp"
        android:id="@+id/textView8"
        android:textColor="#000000"
        android:layout_marginTop="390dp"
        android:layout_marginLeft="20dp" />
    <EditText
        android:layout_width="160dp"
        android:layout_height="40dp"
        android:id="@+id/text3"
        android:textSize="20dp"
        android:textColor="#000000"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="340dp"/>
    <Button
        android:id="@+id/button"
        android:layout_width="160dp"
        android:layout_height="50dp"
        android:background="@drawable/shape"
        android:text="增加数据"
        android:textSize="20dp"
        android:layout_below="@+id/textView"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="450dp" />

</RelativeLayout>

连接手机,界面显示为:

对应的Mainactivity中写的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取各个数据信息
        final EditText nameET =(EditText)findViewById(R.id.name);
        final EditText idET =(EditText)findViewById(R.id.id);
        final EditText floorET =(EditText)findViewById(R.id.floor);
        final EditText text1ET =(EditText)findViewById(R.id.text1);
        final EditText text2ET =(EditText)findViewById(R.id.text2);
        final EditText text3ET =(EditText)findViewById(R.id.text3);
        final EditText text4ET =(EditText)findViewById(R.id.text4);
        //获得按钮控件
        Button login=(Button)findViewById(R.id.button);
        login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //把获取到信息利用起来
                String name=nameET.getText().toString();
                String id=idET.getText().toString();
                String floor=floorET.getText().toString();
                String text1=text1ET.getText().toString();
                String text2=text2ET.getText().toString();
                String text3=text3ET.getText().toString();
                String text4=text4ET.getText().toString();
                FileOutputStream fos=null;
                //抛出异常
                try {
                    fos=openFileOutput("login",MODE_PRIVATE);
                    //把这些信息写入
                    fos.write((name+" "+id+" "+floor+" "+text1+" "+text2+" "+text3+" "+text4).getBytes());
                    fos.flush();//刷新
                }catch (FileNotFoundException e){
                    e.printStackTrace();
                }catch (IOException e){
                    e.printStackTrace();
                }finally {
                    if(fos!=null){
                        try {
                            fos.close();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                }
                Intent intent =new Intent();
                //跳转到InternalDataReadActivity
                intent.setClass(MainActivity.this,InternalDataReadActivity.class);
                startActivity(intent);//实现跳转
            }
        });
    }

此时我写了一个result,用来检测数据是否存入,是不是能调用调用返回我的result这个新界面上。上面的代码也实现了界面的跳转。新界面的XML为(这个时候我用的就是默认布局了,相对布局的好处就是不会因为手机版本不同,位置就发生变化。此时只是为了测试,就没有用相对布局啦):

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/id"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/floor"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text1"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text2"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text3"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text4"
        android:textSize="20sp"/>

</LinearLayout>
为这个界面实现了相应的.Java。名字叫InternalDataReadActivity。由FileInputStream输入流将login文件中的数据显示在result界面中。采用数组byte[ ] buffer搬运,不用一个一个字节搬运。
protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        FileInputStream fis=null;
        byte[] buffer=null;
        try {
            fis=openFileInput("login");
            buffer=new  byte[fis.available()];
            fis.read(buffer);
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        TextView nameTV=(TextView)findViewById(R.id.name);
        TextView idTV=(TextView)findViewById(R.id.id);
        TextView floorTV=(TextView)findViewById(R.id.floor);
        TextView text1TV=(TextView)findViewById(R.id.text1);
        TextView text2TV=(TextView)findViewById(R.id.text2);
        TextView text3TV=(TextView)findViewById(R.id.text3);
        TextView text4TV=(TextView)findViewById(R.id.text4);


        String data=new String(buffer);
        String name=data.split(" ")[0];
        String id=data.split(" ")[1];
        String floor=data.split(" ")[2];
        String text1=data.split(" ")[3];
        String text2=data.split(" ")[4];
        String text3=data.split(" ")[5];
        String text4=data.split(" ")[6];


        nameTV.setText("房间名:"+name);
        idTV.setText("房间号:"+id);
        floorTV.setText("楼层:"+floor);
        text1TV.setText("Xmin:"+text1);
        text2TV.setText("Ymin:"+text3);
        text3TV.setText("Xmax:"+text2);
        text4TV.setText("Ymax:"+text4);
    }

此时测试,会发现可以将输入的数据显示在测试界面上:

                               

也可以找到自己建的login文件,去看看里面有没有本次输入的数据:需要打开Device File Explorer,在View-Tools Windows里面找到File里就会显示文本文件login.txt。但是我发现我这样写每次只能保存一组数据,下一次在输入数据的时候,上一次的数据就没了,无法追加。而我的需求是,要让这个文件夹像一个库一样,存储我输的所有数据。后来我对代码进行了更改,发现OpenFileOutput在打开文件的时候有几个钟不同的模式,Private模式会覆盖原来的数据,可以用Appen模式。就是把MODE_PRIVATE改成MODE_APPEN.此时就是可以继续追加的模式了。大家可以根据自己的需求进行选择。

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值