Android Studio-da menyu yaratish
Menyu elementlarini bosish bilan ishlash
Download 107.5 Kb.
|
- Bu sahifa navigatsiya:
- MainActivity.java
Menyu elementlarini bosish bilan ishlash.Agar menyu elementlaridan birini bosgan bo'lsak, unda hech narsa bo'lmaydi. Amallarni menyuga bog'lash uchun MainActivity.java faylida onOptionsItemSelected-ni bekor qilishimiz kerak. Tanlangan menyu bandini ekranda ko'rsatish uchun activity_main.xml fayliga id = header bilan TextView-ni qo'shamiz: android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" /> a MainActivity.java faylini o'zgartiramiz: package com.example.android.menyuilova; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); TextView headerView = findViewById(R.id.header); switch (id) { case R.id.sozlamalar: headerView.setText("Sozlamalar"); return true; case R.id.ochish: headerView.setText("Ochish"); return true; case R.id.saqlash: headerView.setText("Saqlash"); return true; } return super.onOptionsItemSelected(item); } } Qaysi menyu tanlanganligini aniqlash uchun avval uning identifikatorini olamiz: int id = item.getItemId(). Keyin biz switch, case -dan foydalanamiz va kerakli variantni tanlaymiz va tanlovga qarab ma'lum amallarni bajaramiz - bizni kodda TextView matnini o'rnatadi. Endi yana bir bor ilovani ishga tushirib ishlashini ko'ramiz: Download 107.5 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling