当前位置:操作系统 > 安卓/Android >>

Android[中级教程]第二章 数据存储之File

接着上面一章,这次我们将数据存储在File文件里,布局文件没什么改变,还是一样的布局,看main.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" android:layout_width="match_parent" 

    android:layout_height="match_parent"> 

    <TextView android:layout_width="wrap_content" android:id="@+id/textView1" 

        android:textAppearance="?android:attr/textAppearanceLarge" 

        android:layout_height="wrap_content" android:text="TextView"></TextView> 

    <EditText android:id="@+id/editText1" android:layout_width="match_parent" 

        android:layout_height="wrap_content" android:inputType="number"> 

        <requestFocus></requestFocus> 

    </EditText> 

    <LinearLayout android:layout_width="match_parent" 

        android:id="@+id/linearLayout2" android:layout_height="wrap_content"> 

        <Button android:layout_width="wrap_content" 

            android:layout_height="wrap_content" android:id="@+id/addOne" 

            android:text="悟空又打死了一个妖怪"></Button> 

        <Button android:layout_width="wrap_content" 

            android:layout_height="wrap_content" android:id="@+id/read_edit" 

            android:text="读取Edit中的数据"></Button> 

    </LinearLayout> 

    <LinearLayout android:layout_width="match_parent" 

        android:id="@+id/linearLayout1" android:layout_height="wrap_content"> 

        <Button android:layout_width="wrap_content" 

            android:layout_height="wrap_content" android:id="@+id/save" 

            android:text="存储数据"></Button> 

        <Button android:layout_width="wrap_content" 

            android:layout_height="wrap_content" android:id="@+id/read" 

            android:text="读取数据"></Button> 

        <Button android:layout_width="wrap_content" 

            android:layout_height="wrap_content" android:id="@+id/clear" 

            android:text="清除数据"></Button> 

    </LinearLayout> 

 

</LinearLayout> 

跟上一章的一样,也是一个EditText和几个按钮

 

主java文件:

 

 

import java.io.FileInputStream; 

import java.io.FileNotFoundException; 

import java.io.FileOutputStream; 

import java.io.PrintStream; 

 

import android.app.Activity; 

import android.os.Bundle; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.EditText; 

import android.widget.TextView; 

 

public class Shared_PreferencesDemo extends Activity implements OnClickListener 

    private final String FILE_NAME = "demo.bin"; 

    private int count = 0; 

    private String str; 

    private TextView text; 

    private EditText edit_text; 

    private Button add; 

    private Button save; 

    private Button read; 

    private View clear; 

    private Button read_edit; 

     

    @Override 

    protected void onCreate(Bundle savedInstanceState) 

    { 

        // TODO Auto-generated method stub 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.shared_preferences); 

         

        str = "悟空杀死了" + count + "只妖怪."; 

         

        text = (TextView)findViewById(R.id.textView1); 

        text.setText(str); 

         

        edit_text = (EditText)findViewById(R.id.editText1); 

        edit_text.setText(String.valueOf(count)); 

         

        add = (Button)findViewById(R.id.addOne); 

        add.setOnClickListener(this); 

         

        read_edit = (Button)findViewById(R.id.read_edit); 

        read_edit.setOnClickListener(this); 

         

        save = (Button)findViewById(R.id.save); 

        save.setOnClickListener(this); 

         

        read = (Button)findViewById(R.id.read); 

        read.setOnClickListener(this); 

         

        clear = (Button)findViewById(R.id.clear); 

        clear.setOnClickListener(this); 

         

&nb

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,