Diolog menyular va ulardan foydalanish.
Fragment klassi o'z hayot sikli bilan barcha standart fragment
funktsiyalarini o'z ichiga oladi, ammo DialogFragment sinfidan meros
bo'lib o'tadi, bu esa qo'shimcha funktsiyalarni qo'shadi. Va uni yaratish
uchun biz ikkita usuldan foydalanishimiz mumkin:
● Dialog ob'ektini qaytaradigan onCreateDialog() metodini bekor
qilish.
● Standart onCreateView() metodidan foydalanish.
OnCreateDialog()
metodi
dialog
oynasini
yaratish
uchun
AlertDialog.Builder sinfidan foydalanadi. Uning metodlaridan foydalanib,
siz dialog oynasining ko'rinishini sozlashingiz mumkin:
● setTitle: oynaning sarlavhasini o'rnatadi.
● setView: oyna interfeysi layout-ini o'rnatadi.
● setIcon: oyna ikonkasini o'rnatadi.
● setPositiveButton: amalni tasdiqlash tugmachasini o'rnatadi.
● setNeutralButton: tasdiqlash yoki bekor qilishdan farq qilishi
mumkin bo'lgan "neytral" tugmachani o'rnatadi.
● setNegativeButton: bekor qilish tugmachasini o'rnatadi.
● setMessage: dialog oynasining matnini o'rnatadi, lekin setView-dan
foydalanishda bu usul ixtiyoriy yoki agar biz shunchaki xabarni
ko'rsatishni xohlasak, uni alternativ deb hisoblashimiz mumkin.
● create: oyna yaratadi.
Bizni kodda, dialog oynasida faqat xabar ko'rsatiladi.
Dialog oynasini chaqirish uchun activity_main.xml faylida tugmachani
qo'shing:
version="1.0"
encoding="utf-8"?>
<
androidx.constraintlayout.widget.ConstraintLayout
xmlns:android
="
http://schemas.android.com/apk/res/android
"
xmlns:app
="
http://schemas.android.com/apk/res-auto
"
android:layout_width
="
match_parent
"
android:layout_height
="
match_parent
">
<
Button
android:layout_width
="
wrap_content
"
android:layout_height
="
wrap_content
"
android:onClick
="
showDialog
"
android:text
="
Dialog
"
app:layout_constraintLeft_toLeftOf
="
parent
"
app:layout_constraintTop_toTopOf
="
parent
"
/>
androidx.constraintlayout.widget.ConstraintLayout
>
MainActivity kodida dialog oynasini ishga tushiradigan tugmacha uchun metodni aniqlang:
package
com.example.android.dialogfragment;
import
androidx.appcompat.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
public
class
MainActivity
extends
AppCompatActivity
{
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public
void
showDialog(View
v)
{
CustomDialogFragment
dialog
=
new
CustomDialogFragment();
dialog.show(getSupportFragmentManager(),
"custom");
}
}
Dialog oynasini chaqirish uchun CustomDialogFragment fragment
ob'ekti yaratiladi, so'ngra unga show() metodi chaqiriladi. Ushbu
metodga FragmentManager va teg parametr sifatida beriladi.
Ilovani ishga tushirib natijani ko'ring:
Endi dialog oynasini biroz sozlaymiz(o'zgartiramiz). Bu uchun
CustomDialogFragment sinfidagi onCreateDialog metodini quyidagicha
o'zgartiring:
public
Dialog
onCreateDialog(Bundle
savedInstanceState)
{
AlertDialog.Builder
builder
=
new
AlertDialog.Builder(getActivity());
return
builder
.setTitle("Muloqot
oynasi")
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Oynani
yopish
uchun
OK
tugmasini
bosing")
.setPositiveButton("OK",
null)
.setNegativeButton("Bekor
qilish",
null)
.create();
}
Bu kodda biz android.R.drawable.ic_dialog_alert resursini rasm sifatida
ishlatadigan va ikkita tugmachani o'rnatadigan layoutni qo'shamiz.
Ilovani ishga tushiring va natijani ko'ring:
Endi res/layout papkasiga yangi dialog.xml faylini qo'shing:
version="1.0"
encoding="utf-8"?>
<
androidx.constraintlayout.widget.ConstraintLayout
xmlns:android
="
http://schemas.android.com/apk/res/android
"
xmlns:app
="
http://schemas.android.com/apk/res-auto
"
android:layout_width
="
match_parent
"
android:layout_height
="
match_parent
">
<
TextView
android:layout_width
="
match_parent
"
android:layout_height
="
wrap_content
"
android:gravity
="
center
"
android:text
="
OMUC
"
app:layout_constraintBottom_toBottomOf
="
parent
"
app:layout_constraintLeft_toLeftOf
="
parent
"
app:layout_constraintRight_toRightOf
="
parent
"
app:layout_constraintTop_toTopOf
="
parent
"
/>
androidx.constraintlayout.widget.ConstraintLayout
>
Endi dialog oynasini o'zgartiring. Bu uchun CustomDialogFragment
sinfidagi onCreateDialog metodini quyidagicha o'zgartiring:
package
com.example.android.dialogfragment;
import
android.app.AlertDialog;
import
android.app.Dialog;
import
android.os.Build;
import
android.os.Bundle;
import
androidx.annotation.NonNull;
import
androidx.annotation.RequiresApi;
import
androidx.fragment.app.DialogFragment;
public
class
CustomDialogFragment
extends
DialogFragment
{
@RequiresApi(api
=
Build.VERSION_CODES.LOLLIPOP)
@NonNull
public
Dialog
onCreateDialog(Bundle
savedInstanceState)
{
AlertDialog.Builder
builder
=
new
AlertDialog.Builder(getActivity());
return
builder
.setTitle("Muloqot
oynasi")
.setIcon(android.R.drawable.ic_dialog_alert)
.setView(R.layout.dialog)
.setPositiveButton("OK",
null)
.setNegativeButton("Bekor
qilish",
null)
.create();
}
}
SetView() metodi avval qo'shilgan layout dialog.xml resursini oyna
interfeysi sifatida o'rnatadi. Endi yana ilovani ishga tushiring va natijani
ko'ring:
Do'stlaringiz bilan baham: |