Ushbu darsimizda menular bilan ishlashni o'rganib chiqamiz.
Yangi proyekt yaratamiz File ⇒ New ⇒ Android Project
menu.xml nomli fayl yaratamiz /res/menu/ papkasini ichida.
Agar sizda menu degan papka yo'q bo'lsa, values degan papkani ichiga yaratsangiz ham bo'ladi.
Agar sizda menu degan papka yo'q bo'lsa, values degan papkani ichiga yaratsangiz ham bo'ladi.
menu.xml faylni ichiga quyidagilarni kiritamiz:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_bookmark"
android:icon="@android:drawable/ic_menu_add"
android:title="Bookmark" />
<item android:id="@+id/menu_save"
android:icon="@android:drawable/ic_menu_save"
android:title="Save" />
<item android:id="@+id/menu_search"
android:icon="@
android:
drawable/ic_menu_search"
android:title="Search" />
<item android:id="@+id/menu_share"
android:icon="@
android:
drawable/ic_manu_share"
android:title="Share" />
<item android:id="@+id/menu_delete"
android:icon="@
android:
drawable/ic_menu_delete"
android:title="Delete" />
<item android:id="@+id/menu_preferences"
android:icon="@
android:
drawable/ic_menu_preferences"
android:title="Preferences" />
</menu>
Endi nimaga bularni yozganimizni aytib o'tsak:
<item>/*bu yerga parametrlar yoziladi */</item> yoki <item /*bu yerga parametrlar yoziladi */ /> - agar biz yangi menu yaratmoqchi bo'lsak ushbu <item> tegidan foydalanamiz
android:id="@+id/menu_bookmark" - bu menuning "id" si<item android:id="@+id/menu_bookmark"
android:icon="@android:drawable/ic_menu_add"
android:title="Bookmark" />
android:icon="@android:drawable/ic_menu_add" - bu menuning "icon" i
android:title="Bookmark" - bu menuning "title"si yani chiqib turadigan yozuvi desak ham bo'ladi oddiygina
Endi asosiy Activity classimizni {MenuActivity.java } ni ochamiz va quyidagilarni yozamiz.
package uz.sardordushamov.AndroidMenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import
android.view.Toast
;
public class MenuActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater
.inflate(R.menu.menu, menu); //Bu yerda menu.xml faylimiz turgan joyiga yo'lni ko'rsatamiz
return true;
}
//Bu yerda menular bosilganda nima ish bajarish kerakligini yozamiz
// Hozir biz faqat oddiygina habar chiqaramiz
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.
menu_bookmark:
Toast.makeText(
MenuActivity.this, "Bookmark menusi bosildi", Toast.LENGTH_SHORT).show();
return true;
case R.id.
menu_save
:
Toast.makeText(
MenuActivity.this, "Save menusi bosildi", Toast.LENGTH_SHORT).show();
return true;
case R.id.
menu_
search
:
Toast.makeText(
MenuActivity.this, "Search menusi bosildi", Toast.LENGTH_SHORT).show();
return true;
case R.id.
menu_
share
:
Toast.makeText(
MenuActivity.this, "Share menusi bosildi", Toast.LENGTH_SHORT).show();
return true;
case R.id.
menu_
delete
:
Toast.makeText(
MenuActivity.this, "Delete menusi bosildi", Toast.LENGTH_SHORT).show();
return true;
case R.id.
menu_
preferences
:
Toast.makeText(
MenuActivity.this, "Preferences menusi bosildi", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}}
}
Dasturni ishga tushirishimiz mumkin:
Menulardan birortasini tanlaganimizda quyidagicha natija chiqadi:
Dastur kodini ko'chirib olishingiz mumkin: KO'CHIRIB OLISH
No comments:
Post a Comment