标签:
当然是以文件形式写到手机内存,不过在模拟器上面运行会很慢
布局文件
<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.file.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="文件名称"/> <EditText android:id="@+id/fileName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/textView1"/> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/fileName" android:text="文本内容"/> <!-- 输入yu最少显示3行 --> <EditText android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/textView2" android:minLines="3"/> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/content" android:text="保存"/> <TextView android:id="@+id/fileContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/save" android:text="您输入的内容:"/> </RelativeLayout>保存数据的业务逻辑
package com.example.service; import java.io.FileOutputStream; import android.content.Context; public class FileService { private Context context; public FileService(Context context){ this.context = context; } public void save(String filename,String content){ try { /*openFileOutput * 第一个参数:输入的文件名,不接收路径 * 第二个参数:读取文件的权限 * 应用会默认保存在该工程files文件夹下,其中files文件夹在保存数据后会自动生成 */ FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE); //数据以字节码的方式保存 outStream.write(content.getBytes()); outStream.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.example.file; import com.example.service.FileService; 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; import android.widget.Toast; public class MainActivity extends Activity { private EditText fileName; private EditText content; private Button save; private TextView fileContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fileName = (EditText) findViewById(R.id.fileName); content = (EditText) findViewById(R.id.content); save = (Button) findViewById(R.id.save); fileContent = (TextView) findViewById(R.id.fileContent); save.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub FileService service = new FileService(MainActivity.this); try { service.save(fileName.getText().toString(), content.getText().toString()); Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "保存失败", Toast.LENGTH_SHORT).show(); } String str = "您输入的内容:" + content.getText().toString(); fileContent.setText(str); } }); } }
点击保存,在DDMS视图下的date/data找到自己的工程。在工程下会生成一个files文件夹,保存的数据文件就在改文件夹下,可点击右上角的图标导出查看
标签:
原文地址:http://blog.csdn.net/lindonglian/article/details/43191629