Кнопки изменения размера текста с сохранением
This commit is contained in:
1 parent
5c0fff3d04
commit
0762bfffbf
8 files changed
+122
-17
No files matched your search
@@ -23,4 +23,9 @@ class Informaition : AppCompatActivity() {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
TextSizeManager.applyTo(findViewById(R.id.main))
|
||||
}
|
||||
}
|
||||
@@ -43,4 +43,9 @@ class MainActivity : AppCompatActivity() {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
TextSizeManager.applyTo(findViewById(R.id.main))
|
||||
}
|
||||
}
|
||||
@@ -24,4 +24,9 @@ class Raspisanie : AppCompatActivity() {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
TextSizeManager.applyTo(findViewById(R.id.main))
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,17 @@
|
||||
package com.example.helponizer
|
||||
|
||||
import android.content.Intent
|
||||
import android.media.MediaPlayer
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import android.media.MediaPlayer
|
||||
|
||||
class Settings : AppCompatActivity() {
|
||||
private var player: MediaPlayer? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
@@ -20,21 +21,37 @@ class Settings : AppCompatActivity() {
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||
insets
|
||||
}
|
||||
|
||||
findViewById<View>(R.id.buttonSound).setOnClickListener {
|
||||
player?.release()
|
||||
player = MediaPlayer.create(this, R.raw.click)
|
||||
player?.start()
|
||||
}
|
||||
|
||||
findViewById<View>(R.id.buttonTextUp).setOnClickListener {
|
||||
TextSizeManager.increase(this)
|
||||
TextSizeManager.applyTo(findViewById(R.id.main))
|
||||
}
|
||||
|
||||
findViewById<View>(R.id.buttonTextDown).setOnClickListener {
|
||||
TextSizeManager.decrease(this)
|
||||
TextSizeManager.applyTo(findViewById(R.id.main))
|
||||
}
|
||||
|
||||
findViewById<View>(R.id.buttonOnMain).setOnClickListener {
|
||||
startActivity(Intent(this, MainActivity::class.java))
|
||||
finish()
|
||||
findViewById<View>(R.id.buttonSound).setOnClickListener {
|
||||
player?.release()
|
||||
player = MediaPlayer.create(this, R.raw.click)
|
||||
player?.start()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
override fun onDestroy(){
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
TextSizeManager.applyTo(findViewById(R.id.main))
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
player?.release()
|
||||
player = null
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.example.helponizer
|
||||
|
||||
import android.content.Context
|
||||
import android.util.TypedValue
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
|
||||
object TextSizeManager {
|
||||
private const val PREFS_NAME = "helponizer_prefs"
|
||||
private const val KEY_SCALE = "text_scale"
|
||||
private const val MIN_SCALE = 0.8f
|
||||
private const val MAX_SCALE = 2.0f
|
||||
private const val STEP = 0.1f
|
||||
private const val TAG_ORIGINAL_SIZE = -0x54a1 // id для сохранения исходного размера
|
||||
|
||||
private var scale: Float? = null
|
||||
|
||||
fun getScale(context: Context): Float {
|
||||
if (scale == null) {
|
||||
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
scale = prefs.getFloat(KEY_SCALE, 1f).coerceIn(MIN_SCALE, MAX_SCALE)
|
||||
}
|
||||
return scale!!
|
||||
}
|
||||
|
||||
fun increase(context: Context): Float = change(context, STEP)
|
||||
|
||||
fun decrease(context: Context): Float = change(context, -STEP)
|
||||
|
||||
private fun change(context: Context, delta: Float): Float {
|
||||
val newScale = (getScale(context) + delta).coerceIn(MIN_SCALE, MAX_SCALE)
|
||||
scale = newScale
|
||||
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.edit()
|
||||
.putFloat(KEY_SCALE, newScale)
|
||||
.apply()
|
||||
return newScale
|
||||
}
|
||||
|
||||
fun applyTo(root: View) {
|
||||
applyToView(root, getScale(root.context))
|
||||
}
|
||||
|
||||
private fun applyToView(view: View, scale: Float) {
|
||||
if (view is TextView) {
|
||||
val original = view.getTag(TAG_ORIGINAL_SIZE) as? Float
|
||||
?: view.textSize.also { view.setTag(TAG_ORIGINAL_SIZE, it) }
|
||||
val target = original * scale
|
||||
if (view.textSize != target) {
|
||||
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, target)
|
||||
}
|
||||
}
|
||||
if (view is ViewGroup) {
|
||||
for (i in 0 until view.childCount) {
|
||||
applyToView(view.getChildAt(i), scale)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,4 +24,9 @@ class infoProgram : AppCompatActivity() {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
TextSizeManager.applyTo(findViewById(R.id.main))
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/platine"
|
||||
android:fillViewport="true"
|
||||
tools:context=".infoProgram">
|
||||
tools:context=".Settings">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
@@ -23,7 +23,7 @@
|
||||
<TextView
|
||||
style="@style/ScreenTitle"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Настройки"
|
||||
android:text="@string/settings_title"
|
||||
android:gravity="center"/>
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Сменить палитру"
|
||||
android:text="@string/settings_change_palette"
|
||||
android:textSize="15sp"/>
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/buttonSound"
|
||||
@@ -46,7 +46,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Воспроизвести звук"
|
||||
android:text="@string/settings_play_sound"
|
||||
android:textSize="15sp"/>
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/buttonTextUp"
|
||||
@@ -54,7 +54,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Увеличить текст"
|
||||
android:text="@string/settings_text_up"
|
||||
android:textSize="15sp"/>
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/buttonTextDown"
|
||||
@@ -62,7 +62,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Уменьшить текст"
|
||||
android:text="@string/settings_text_down"
|
||||
android:textSize="15sp"/>
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/buttonNotification"
|
||||
@@ -71,7 +71,7 @@
|
||||
android:layout_height="50dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:text="Включить уведомление"
|
||||
android:text="@string/settings_notification"
|
||||
android:textSize="15sp"/>
|
||||
</LinearLayout>
|
||||
<com.google.android.material.button.MaterialButton
|
||||
|
||||
@@ -40,6 +40,14 @@
|
||||
<string name="about_description">"Приложение создано для помощи студентам \n в организации учебного процесса. Оно \nпозволяет просматривать расписание занятий \nи знакомиться с полезными рекомендациями \nпо обучению."</string>
|
||||
<string name="about_developer">"Разработчик:\nЧесаков Никита Сергеевич\nГруппа: ИСП-24/9\n\nГод разработки: 2026"</string>
|
||||
|
||||
<!-- Settings screen -->
|
||||
<string name="settings_title">Настройки</string>
|
||||
<string name="settings_change_palette">Сменить палитру</string>
|
||||
<string name="settings_play_sound">Воспроизвести звук</string>
|
||||
<string name="settings_text_up">A+ Увеличить текст</string>
|
||||
<string name="settings_text_down">A− Уменьшить текст</string>
|
||||
<string name="settings_notification">Включить уведомление</string>
|
||||
|
||||
<!-- Common -->
|
||||
<string name="button_back_to_main">На главную</string>
|
||||
</resources>
|
||||
Reference in new issue
Block a user