Академический Документы
Профессиональный Документы
Культура Документы
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.commit
import androidx.fragment.app.replace
<Button
android:id="@+id/frag1Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnChecker1"
android:text="@string/fragment1" />
<Button
android:id="@+id/frag2Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnChecker2"
android:text="@string/fragment2" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Листинг string.xml
<resources>
<string name="app_name">Lab3</string>
<string name="fragment2">Fragment 2</string>
<string name="fragment1">Fragment 1</string>
</resources>
Листинг build.gradle.kts
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.example.lab3"
compileSdk = 34
defaultConfig {
applicationId = "com.example.lab3"
minSdk = 23
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("androidx.fragment:fragment-ktx:1.3.6")
}
Листинг FragmentFirst.kt
package com.example.lab3
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class FirstFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false)
}
}
Листинг fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/fragment1" />
</FrameLayout>
Листинг SecondFragment.kt
package com.example.lab3
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class SecondFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false)
}
}
Листинг fragment_second.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_purple">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/fragment2" />
</FrameLayout>
Рис 1. Получившееся приложение
Рис 2. Первый фрагмент
Рис 3. Второй фрагмент
Дополнительные задания:
1. Реализовать возможность смены фрагментов при помощи одной кнопки.
2. Установить запрет скриншота на разработанное приложение
3. На одном Fragments расположить дополнительный текст с фамилиями
студентов из бригады, на другом расположить номер группы
Листинг MainActivity.kt
package com.example.lab3
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import androidx.fragment.app.commit
import androidx.fragment.app.replace
Листинг activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/frag1Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="ChangeBtn"
android:text="Change fragment" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Листинг fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="IKTB-27M"
android:textSize="32dp"
android:gravity="center"
android:textColor="@color/black"/>
</FrameLayout>
Листинг fragment_second.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_purple">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Nechaev Andrey"
android:textSize="32dp"
android:gravity="center"
android:textColor="@color/black"/>
</FrameLayout>
Рис 4. Получившееся приложение
Рис 5. Первый фрагмент
Рис 6. Второй фрагмент
Рис 7. Репозиторий на Github
Вывод:
Изучены возможности и инструменты среды разработки Android Studio.
Изучены принципы создания и работы компонентов Activity и Fragment.
Произведено ознакомление с правилами создания их файлов разметки.