Вы находитесь на странице: 1из 17

МИНИСТЕРСТВО ЦИФРОВОГО РАЗВИТИЯ, СВЯЗИ И МАССОВЫХ

КОММУНИКАЦИЙ РОССИЙСКОЙ ФЕДЕРАЦИИ


ФЕДЕРАЛЬНОЕ ГОСУДАРСТВЕННОЕ БЮДЖЕТНОЕ ОБРАЗОВАТЕЛЬНОЕ
УЧРЕЖДЕНИЕ ВЫСШЕГО ОБРАЗОВАНИЯ
САНКТ-ПЕТЕРБУРГСКИЙ ГОСУДАРСТВЕННЫЙ УНИВЕРСИТЕТ
ТЕЛЕКОММУНИКАЦИЙ
ИМ. ПРОФ. М. А. БОНЧ-БРУЕВИЧА

Кафедра защищенных систем связи

Отчет по лабораторной работе


“Разработка приложения на Android, Fragments”

по дисциплине «Технологии защиты беспроводных сетей и мобильных


приложений»

Выполнил студент гр. -27


Нечаев А.А.
Принял к.т.н. , доцент кафедры
ЗСС
Ковцур М. М.
Цель работы:
Реализовать мобильное приложение, содержащее два Fragment,
располагающихся в Activity с возможностью переключения между ними при
помощи нажатия соответствующих кнопок.
Выполнение:
Листинг MainActivity.kt:
package com.example.lab3

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.commit
import androidx.fragment.app.replace

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

fun btnChecker1(view: android.view.View) {


supportFragmentManager.commit {
replace<FirstFragment>(R.id.fragmentContainerView)
}
}
fun btnChecker2(view: android.view.View) {
supportFragmentManager.commit {
replace<SecondFragment>(R.id.fragmentContainerView)
}
}
}
Листинг 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="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

class MainActivity : AppCompatActivity() {


private var change_fragment : Boolean = true

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE)
}

fun ChangeBtn(view: android.view.View) {


if (change_fragment) {
supportFragmentManager.commit {
replace<FirstFragment>(R.id.fragmentContainerView)}
change_fragment = false
} else {
supportFragmentManager.commit {
replace<SecondFragment>(R.id.fragmentContainerView)}
change_fragment = true
}
}
}

Листинг 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.
Произведено ознакомление с правилами создания их файлов разметки.

Вам также может понравиться