10-dars. Androidda oddiy menu yaratish.
Menu tushunchasi hammamizga ma’lum. Bilamizki keng imkonyatli dastur yaratmoqchi bo’lsak albatda menudan foydalanishimizga to’g’ri keladi. Android qurulmalarida menu tugmasi bosilganda chiqadigan menularni qanday hosil qilish haqida o’rganamiz.
Yangi loyiha yaratamiz.
Application name: Android_menu
Project Name: Android_menu
Package Name: uz.projects.android_menu
Target SDK: API 17 Android 4.2(Jelly Bean)
Create Activity: MainActivity
Activity_main.xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</RelativeLayout>
MainActivity.java ni ochamiz va onCreateOptionsMenu metodiga o’tamiz. Ushbu metodning kiruvchi parametirida Menu classining menu obyekti berilgan. Biz ushbu menu obyektining add metodidan foydalanib menular qo’shamiz va add metodida kiruvchi parameter tekst turudadir. onCreateOptionsMenu metodimiz Boolean tipidagi True va False qiymatlarini qaytaradi. Agar rost bo’lsa menuyimiz ko’rinadi aks holda ko’rinmaydi.
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add("projects.uz");
menu.add("Opennet.uz");
return super.onCreateOptionsMenu(menu);
}
Tekshirib ko’ramiz:
Applicationimiz muffaqiyatli ishlamoqda. Lekin bu menularda onclick metodi yo’q.
Shuning uchun ham bosganda hech qanday amal yoki vazifa bajarilayotgani ham yo’q. Menularimizning click hodisasini amalga oshirishimiz uchun onOptionsItemSelected metodidan foydalanamiz. Bu metodning kiruvchi parametiriga MenuItem classining item obyektini qiymat qilib beramiz. Ya’ni ushbu obyekt bizga qaysi menu bosilganligini aniqlab beradi.
Biz ushbu appimizda bosilgan menuning nomini toast orqaliy ekranga chiqaramiz. Bunda MenuItem classining item obyektining getTitle metodi yordom beradi.
onOptionsItemSelected metdimiz Boolean tipida bo’lganligi uchun true va false qiymatlarni qabul qiladi. Super classi orqaliy metodimizni returniga true qiymat qaytarayapmiz.
public boolean onOptionsItemSelected(MenuItem item)
{
Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
}
Full code:
package uz.projects.android_menu;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add("projects.uz");
menu.add("Opennet.uz");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item)
{
Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
}
}
9-dars. Androidda xabarlar bilan ishlash.
Maqolani yozishda startandroid.ru saytidan foydalanildi.
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tanlov uchunmi yoki tanlov uchunmasmi buni ahamiyati yuq. asosiysi yozilmoqda!
undan tashqari bu startandroid.ru tarjimalari!
Siz kutayotganday sifatli maqolalar yozishimizga «sal»gina bor deb o'ylayman. shu ketish bo'lsa o'sha «sal» ancha yaqinlashtiramiz.