2、编写一个app,再EditText中输入内容,并写入文件。点击按钮读出文件内容。演示过程并在ddms中查看数据存储。
时间: 2024-03-25 09:41:50 浏览: 74
Android TextWatcher监控EditText中的输入内容并限制其个数
好的,我可以为您提供这个App的示例代码。请注意,这个App将会在Internal Storage中创建一个名为“example.txt”的文件,将EditText中输入的内容写入该文件中。当您点击按钮时,它将从文件中读取内容并在TextView中显示。以下是示例代码:
MainActivity.java:
```java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editText;
private TextView textView;
private static final String FILENAME = "example.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView);
Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveToFile(editText.getText().toString());
}
});
Button loadButton = (Button) findViewById(R.id.loadButton);
loadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = readFromFile();
if (text != null) {
textView.setText(text);
} else {
Toast.makeText(MainActivity.this, "Failed to load data!", Toast.LENGTH_SHORT).show();
}
}
});
}
private void saveToFile(String text) {
try {
FileOutputStream fos = openFileOutput(FILENAME, MODE_PRIVATE);
fos.write(text.getBytes());
fos.close();
Toast.makeText(this, "Data saved to file!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Failed to save data!", Toast.LENGTH_SHORT).show();
}
}
private String readFromFile() {
try {
FileInputStream fis = openFileInput(FILENAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
isr.close();
fis.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
```
activity_main.xml:
```xml
<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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:padding="10dp"
android:hint="Enter text here" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:text="Save to file" />
<Button
android:id="@+id/loadButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/saveButton"
android:text="Load from file" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/loadButton"
android:layout_marginTop="10dp"
android:padding="10dp"
android:text="Data will be loaded here" />
</RelativeLayout>
```
请注意,您需要在AndroidManifest.xml文件中为您的应用程序添加文件读写权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
在使用此应用程序时,请确保在DDMS中查看数据存储。您可以在Android Studio中打开DDMS视图,然后切换到File Explorer选项卡,以查看应用程序的数据目录。在本例中,您应该能够看到一个名为“app_example”的目录,其中包含名为“example.txt”的文件。
阅读全文