Files
labs-mobile-applications/app/src/main/java/com/example/myapplication/MainActivity.kt
T

55 lines
1.6 KiB
Kotlin
Raw Normal View History

2025-09-24 12:26:38 +04:00
package com.example.myapplication
import android.os.Bundle
2025-09-24 14:18:44 +04:00
import android.widget.Button
import android.widget.EditText
2025-09-24 14:01:30 +04:00
import android.widget.TextView
2025-09-24 13:41:40 +04:00
import android.widget.Toast
2025-09-24 13:25:08 +04:00
import androidx.activity.ComponentActivity
2025-09-24 14:18:44 +04:00
import java.io.File
2025-09-24 12:26:38 +04:00
2025-09-24 13:25:08 +04:00
class MainActivity : ComponentActivity() {
2025-09-24 13:41:40 +04:00
2025-09-24 14:18:44 +04:00
private lateinit var etInput: EditText
private lateinit var btnSave: Button
private lateinit var btnLoad: Button
private lateinit var tvResult: TextView
private val fileName = "myfile.txt"
2025-09-24 13:41:40 +04:00
2025-09-24 12:26:38 +04:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
2025-09-24 13:25:08 +04:00
2025-09-24 14:18:44 +04:00
etInput = findViewById(R.id.etInput)
btnSave = findViewById(R.id.btnSave)
btnLoad = findViewById(R.id.btnLoad)
tvResult = findViewById(R.id.tvResult)
btnSave.setOnClickListener { saveToFile() }
btnLoad.setOnClickListener { loadFromFile() }
2025-09-24 14:01:30 +04:00
}
2025-09-24 14:18:44 +04:00
private fun saveToFile() {
val text = etInput.text.toString()
if (text.isEmpty()) {
Toast.makeText(this, "Введите текст", Toast.LENGTH_SHORT).show()
return
}
val file = File(filesDir, fileName)
file.writeText(text)
Toast.makeText(this, "Файл сохранён", Toast.LENGTH_SHORT).show()
}
2025-09-24 14:01:30 +04:00
2025-09-24 14:18:44 +04:00
private fun loadFromFile() {
val file = File(filesDir, fileName)
if (file.exists()) {
val content = file.readText()
tvResult.text = "Содержимое файла: $content"
} else {
tvResult.text = "Файл не найден"
}
2025-09-24 12:26:38 +04:00
}
}