Android OS - Menu

Sardor Dushamov Android menu

Oldingi maqolalarda biz, ListView, EditText kabi komponentalar bilan ishlashni ko'rib chiqqan edik. Ushbu maqolada biz menu bilan ishlashni o'rganib chiqishga harakat qilamiz.
Menu — togrisi bunga qanday ta'rif berishni bilmadim, chunki menu ni biz juda ham ko'p joylarda eshitganmiz, ko'rganmiz, nimaligini yaxshi bilamiz.


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.

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:
/*bu yerga parametrlar yoziladi */ yoki <item /*bu yerga parametrlar yoziladi */ /> — agar biz yangi menu yaratmoqchi bo'lsak ushbu tegidan foydalanamiz.

   <item android:id="@+id/menu_bookmark"
          android:icon="@android:drawable/ic_menu_add"
          android:title="Bookmark" />


android:id="@+id/menu_bookmark"
— bu menuning «id» si
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:

Sardor Dushamov Android Menu

Dastur kodini ko'chirib olishingiz mumkin: KO'CHIRIB OLISH

1 комментарий

TiTaNIK
qayerga yozishni bilmadim iloji bo'lsa Java o'yinlar, dasturlarni tarjima qilish xaqida maqola yozinglar
0