10. Андроид иловасида диалог ойналарини ҳосил қилиш Dialog fragmenti DialogFragment
setSingleChoiceitems( ) usulidan foydalaning
Download 0.58 Mb. Pdf ko'rish
|
1-10
setSingleChoiceitems( ) usulidan foydalaning .
// Kotlin paket en.alexanderkimov.dialog android.app.Dialogni import qiling android.os.Bundle-ni import qiling import android.widget.Toast androidx.appcompat.app.AlertDialog-ni import qiling androidx.fragment.app.DialogFragmentni import qiling sinf MyDialogFragment: DialogFragment() { private val catNames = arrayOf( " Vaska ", " Ryjik ", " Murzik ") fun onCreateDialog(savedInstanceState: Bundle?): Dialog { faoliyatni qaytarish?.let { val quruvchi = AlertDialog.Builder(bu) builder.setTitle ("Sevimli mushukingiz nomini tanlang") .setSingleChoiceItems(catNames, -1 ) { dialog oynasi, element -> Toast.makeText(faoliyat, " Sevimli nomi mushuk : ${catNames[item]}", Tost.LENGTH_SHORT).show() 15 } .setPositiveButton("OK" ) { dialog, id -> // Foydalanuvchi OK tugmasini bosdi, shuning uchun tanlanganItems natijalarini biror joyda saqlang // yoki ularni dialog oynasini ochgan komponentga qaytaring } .setNegativeButton (" Bekor qilish ") { dialog oynasi, id -> } builder.create() } ?: otish IllegalStateException("Faoliyat null bo'lishi mumkin emas") } } //Java @Override Public Dialog onCreateDialog (to'plam savedInstanceState) { final String[ ] catNamesArray = {" Vaska ", " Rijik ", " Murzik "}; AlertDialog.Builder quruvchisi = yangi AlertDialog.Builder(getActivity()); builder.setTitle ("Sevimli mushukingiz nomini tanlang") // qo'shish kalitlari .setSingleChoiceItems(catNamesArray, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog oynasi, int element) { toast.makeText( getActivity(), " Sevimli nomi mushuk : " +catNamesArray[item], Toast.LENGTH_SHORT).show(); } }) .setPositiveButton("OK", yangi DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // Foydalanuvchi OK tugmasini bosdi, shuning uchun mSelectedItems natijalarini biror joyda saqlang // yoki ularni dialog oynasini ochgan komponentga qaytaring } }) .setNegativeButton (" Bekor qilish ", yangi DialogInterface.OnClickListener() { 16 @Override public void onClick(DialogInterface dialog, int id) { } }); builder.create ( ); } Quyidagi tafsilotlarga e'tibor bering. Radio tugmani tanlash dialog oynasini yopmaydi. Shuning uchun oynani yopish mexanizmini ta'minlash kerak, masalan, tugmachani qo'shish. Ikkinchi nuqta - setSingleChoiceitems usulida birinchi parametr uchun radio tugmalar uchun qiymatlar massivi, ikkinchi parametr uchun esa sukut bo'yicha yoqilgan radio tugma indeksining butun qiymati ishlatiladi. dialog oynasi chaqirilganda, ishlatiladi. Ishga tushganda barcha kalitlar o'chirilgan bo'lishini istasangiz, -1 dan foydalaning. 17 Belgilash qutilari bilan AlertDialog Agar siz bir nechta tanlash uchun radio tugmalari o'rniga belgilash katakchalaridan foydalanmoqchi bo'lsangiz, setMultiChoiceItems ( ) usuli kerak bo'ladi . Kod oldingi misol bilan deyarli bir xil: // Kotlin paket uz.alexanderkimov .dialog android.app.Dialogni import qiling android.os.Bundle-ni import qiling android.util.Log-ni import qiling import android.widget.Toast androidx.appcompat.app.AlertDialog-ni import qiling androidx.fragment.app.DialogFragmentni import qiling sinf MyDialogFragment: DialogFragment() { private val catNames = arrayOf( " Vaska ", " Ryjik ", " Murzik ") private val checkedItems = booleanArrayOf (noto'g'ri, rost, noto'g'ri) fun onCreateDialog(savedInstanceState: Bundle?): Dialog { faoliyatni qaytarish?.let { val quruvchi = AlertDialog.Builder(bu) builder.setTitle (" Tanlang mushuklar ") .setMultiChoiceItems( 18 catNames, checkedItems ) { _, qaysi, tekshiriladi -> checkedItems[qaysi] = isChecked // val nomi = catNames[qaysi] // Bosilgan elementni oling // println (ism) } .setPositiveButton( " Bajarildi " ) { _, _ -> uchun (i catNames.indices da) { val tekshirildi = checkedItems[i] agar (belgilangan) { println(mushuk nomlari[i]) } } } .setNegativeButton( " Bekor qilish " ) { dialog oynasi, _ -> dialog.cancel() } builder.create() } ?: otish IllegalStateException("Faoliyat null bo'lishi mumkin emas") } } //Java @Override Public Dialog onCreateDialog (to'plam savedInstanceState) { final String[ ] catNamesArray = {" Vaska ", " Rijik ", " Murzik "}; final boolean[] checkedItemsArray = {noto'g'ri, rost, noto'g'ri}; AlertDialog.Builder quruvchisi = yangi AlertDialog.Builder(getActivity()); builder.setTitle (" Tanlang mushuklar ") .setMultiChoiceItems(catNamesArray, checkedItemsArray, yangi DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog oynasi, int which, boolean isChecked) { checkedItemsArray[qaysi] = isChecked; } }) .setPositiveButton (“ Bajarildi ”, new DialogInterface.OnClickListener() { @Override 19 public void onClick(DialogInterface dialog oynasi, int identifikatori) { StringBuilder holati = new StringBuilder(); uchun (int i = 0; i < catNamesArray.length; i++) { state.append(catNamesArray[i]); agar (checkedItemsArray[i]) state.append (" tanlangan \n"); boshqa state.append (" emas tanlangan \n"); } toast.makeText(getActivity(), state.toString(), Toast.LENGTH_LONG) .show(); } }) .setNegativeButton (“ Bekor qilish ”, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog oynasi, int identifikatori) { dialog.cancel(); } }); builder.create(); } Download 0.58 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling