写出Mainactivity文件和布局文件实现:a)文件写入,在输入框中输入内容,在文件名输入框中输入文件名,点击SAVE按钮,弹出文件保存成功信息提示。 b)文件读出,点击READ按钮,弹出app私有目录下的文件列表,选择列表项,读取文件内容和文件名在内容显示框文件名显示框中显示。a)在上述的布局下,增加PICTURE按钮和图片显示控件,点击按钮,显示照片选择页面,选择照片后,将照片现在图片显示控件上。
时间: 2023-12-31 18:03:29 浏览: 142
同一个activity中通过一个按钮切换两个不同的布局
5星 · 资源好评率100%
MainActivity.java:
```
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText editTextContent, editTextFileName;
Button buttonSave, buttonRead, buttonPicture;
TextView textViewContent, textViewFileName;
private static final int REQUEST_CODE_SELECT_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextContent = findViewById(R.id.editTextContent);
editTextFileName = findViewById(R.id.editTextFileName);
buttonSave = findViewById(R.id.buttonSave);
buttonRead = findViewById(R.id.buttonRead);
buttonPicture = findViewById(R.id.buttonPicture);
textViewContent = findViewById(R.id.textViewContent);
textViewFileName = findViewById(R.id.textViewFileName);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = editTextContent.getText().toString();
String fileName = editTextFileName.getText().toString();
if (content.isEmpty() || fileName.isEmpty()) {
Toast.makeText(getApplicationContext(), "请输入内容和文件名", Toast.LENGTH_SHORT).show();
} else {
saveFile(fileName, content);
}
}
});
buttonRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileList();
}
});
buttonPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectPicture();
}
});
}
private void saveFile(String fileName, String content) {
File file = new File(getExternalFilesDir(null), fileName);
try {
OutputStream outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
outputStream.close();
Toast.makeText(getApplicationContext(), "文件保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
private void showFileList() {
File dir = getExternalFilesDir(null);
File[] files = dir.listFiles();
List<String> fileNames = new ArrayList<>();
for (File file : files) {
if (file.isFile()) {
fileNames.add(file.getName());
}
}
String[] fileNameArray = new String[fileNames.size()];
fileNameArray = fileNames.toArray(fileNameArray);
FileListAdapter fileListAdapter = new FileListAdapter(this, fileNameArray);
fileListAdapter.setOnItemClickListener(new FileListAdapter.OnItemClickListener() {
@Override
public void onItemClick(String fileName) {
showFileContent(fileName);
}
});
FileListDialogFragment fileListDialogFragment = new FileListDialogFragment(fileListAdapter);
fileListDialogFragment.show(getSupportFragmentManager(), "fileListDialog");
}
private void showFileContent(String fileName) {
File file = new File(getExternalFilesDir(null), fileName);
try {
InputStream inputStream = getResources().getAssets().open(file.getName());
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
String content = new String(buffer, "UTF-8");
textViewFileName.setText(fileName);
textViewContent.setText(content);
} catch (IOException e) {
e.printStackTrace();
}
}
private void selectPicture() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_SELECT_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SELECT_PICTURE && resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageURI(uri);
}
}
}
```
activity_main.xml:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editTextContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入文件内容" />
<EditText
android:id="@+id/editTextFileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入文件名" />
<Button
android:id="@+id/buttonSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVE" />
<Button
android:id="@+id/buttonRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="READ" />
<TextView
android:id="@+id/textViewFileName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonPicture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PICTURE" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:visibility="gone" />
</LinearLayout>
```
阅读全文