写代码android studio代码记事本,文件名是book1版本是2022.2.1

时间: 2024-02-22 22:57:46 浏览: 20
很好,以下是在 Android Studio 中创建一个名为 "book1" 的记事本应用程序的步骤,版本为 2022.2.1: 1. 打开 Android Studio,点击 "Start a new Android Studio project"。 2. 在 "Choose your project" 窗口中,选择 "Empty Activity",然后点击 "Next"。 3. 在 "Configure your project" 窗口中,输入 "book1" 作为应用程序的名称,选择 "Java" 作为语言,选择 "API 30: Android 11.0 (R)" 作为最低 SDK 版本,然后点击 "Finish"。 4. 在项目结构中,找到 "MainActivity.java" 文件,并用下面的代码替换它: ``` import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { EditText editText; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); textView = findViewById(R.id.textView); Button saveButton = findViewById(R.id.saveButton); saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { saveToFile(); } }); Button loadButton = findViewById(R.id.loadButton); loadButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { loadFromFile(); } }); } private void saveToFile() { try { String text = editText.getText().toString(); File file = new File(getFilesDir(), "book1.txt"); FileOutputStream outputStream = new FileOutputStream(file); outputStream.write(text.getBytes()); outputStream.close(); textView.setText("Text saved to " + file.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } } private void loadFromFile() { try { File file = new File(getFilesDir(), "book1.txt"); FileInputStream inputStream = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line).append("\n"); } reader.close(); inputStream.close(); textView.setText(builder.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` 5. 在 "res/layout" 目录下,找到 "activity_main.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"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:hint="Enter text here..." /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/saveButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" /> <Button android:id="@+id/loadButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Load" /> </LinearLayout> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="No text loaded." /> </LinearLayout> ``` 6. 运行应用程序并测试它是否正常工作。现在,您应该可以输入文本并将其保存到名为 "book1.txt" 的文件中,然后从文件中加载文本。

相关推荐

最新推荐

recommend-type

解决Android Studio Log.v和Log.d不显示的问题

主要介绍了解决Android Studio Log.v和Log.d不显示的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

解决Android Studio 代码自动提示突然失效的问题

主要介绍了解决Android Studio 代码自动提示突然失效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

解决Android Studio 代码无提示无颜色区分问题

主要介绍了解决Android Studio 代码无提示无颜色区分问题,需要的朋友可以参考下
recommend-type

AndroidStudio4.0日志中文乱码问题

打开AndroidStudio安装目录,找到bin下这两个文件 最后一行添加 -Dfile.encoding=UTF-8 保存,重启,ok Android Studio 4.0 Run窗口中文乱码解决办法 解决方法: 依次打开菜单:Help–Edit Custom VM Options...
recommend-type

android studio打印日志语句Log.d()详解

主要介绍了android studio打印日志语句Log.d()详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。