This commit is contained in:
Ваше Имя
2025-09-24 13:41:40 +04:00
parent c55b7fde32
commit b2f1553554
3 changed files with 55 additions and 25 deletions
+3
View File
@@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
@@ -1,27 +1,62 @@
package com.example.myapplication
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
class MainActivity : ComponentActivity() {
// Запрос разрешения на контакты
private val requestContactsPermission = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
Toast.makeText(this, "Доступ к контактам разрешён", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Доступ к контактам запрещён", Toast.LENGTH_SHORT).show()
}
}
// Запрос разрешения на галерею
private val requestGalleryPermission = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
Toast.makeText(this, "Доступ к галерее разрешён", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Доступ к галерее запрещён", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val etInput = findViewById<EditText>(R.id.etInput)
val btnApply = findViewById<Button>(R.id.btnApply)
val tvResult = findViewById<TextView>(R.id.tvResult)
val btnContacts = findViewById<Button>(R.id.btnContacts)
val btnGallery = findViewById<Button>(R.id.btnGallery)
btnApply.setOnClickListener {
val text = etInput.text.toString().trim()
if (text.isEmpty()) {
tvResult.text = "Введите текст!"
// При нажатии на кнопку "Контакты"
btnContacts.setOnClickListener {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Уже есть доступ к контактам", Toast.LENGTH_SHORT).show()
} else {
tvResult.text = "РЕЗУЛЬТАТ: ${text.uppercase()}"
requestContactsPermission.launch(Manifest.permission.READ_CONTACTS)
}
}
// При нажатии на кнопку "Галерея"
btnGallery.setOnClickListener {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES)
== PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Уже есть доступ к галерее", Toast.LENGTH_SHORT).show()
} else {
requestGalleryPermission.launch(Manifest.permission.READ_MEDIA_IMAGES)
}
}
}
+6 -14
View File
@@ -4,24 +4,16 @@
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/etInput"
<Button
android:id="@+id/btnContacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Введите текст" />
android:text="Доступ к контактам" />
<Button
android:id="@+id/btnApply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Преобразовать" />
<TextView
android:id="@+id/tvResult"
android:id="@+id/btnGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Здесь будет результат"
android:textSize="18sp" />
android:layout_marginTop="16dp"
android:text="Доступ к галерее" />
</LinearLayout>