Android Option Menu là các menu chính của Android. Chúng có thể được sử dụng để cài đặt, tìm kiếm, xóa mục, .... Ở đây, chúng ta sẽ xem xét hai ví dụ của các menu tùy chọn. Đầu tiên, các menu lựa chọn đơn giản nhất và thứ hai, tùy chọn menu bằng hình ảnh. Ở đây, chúng ta đang lạm phát thực đơn bằng cách gọi phương pháp inflate() của lớp MenuInflater . Để thực hiện việc xử lý sự kiện trên các mục menu, bạn cần phải ghi đè onOptionsItemSelected() của lớp Activity. Ví dụ Android Option Menu Chúng ta hãy xem làm thế nào để tạo menu trong hoc lập trình android. Hãy xem ví dụ tùy chọn menu đơn giản có chứa ba mục trình đơn. activity_main.xml Chúng tôi chỉ có một TextView trong tập tin này. File: activity_main.xml Code: <RelativeLayout xmlns:androclass="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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout> menu_main.xml. Nó chứa ba mục như hiển thị bên dưới. Nó được tạo ra tự động bên trong thư mục res/menu. File: menu_main.xml Code: <menu xmlns:androclass="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:title="Item 1"/> <item android:id="@+id/item2" android:title="Item 2"/> <item android:id="@+id/item3" android:title="Item 3"/> </menu> [Khóa hoc lap trinh android cơ bản tại hà nội !' Lớp Activity Lớp này sẽ hiển thị các nội dung của tập tin menu.xml và thực hiện xử lý sự kiện vào cách nhấp vào mục menu. File: MainActivity.java Code: package com.javatpoint.optionmenu; 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. getMenuInflater().inflate(R.menu.main, menu);//Menu Resource, Menu return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.item1: Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show(); return true; case R.id.item2: Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show(); return true; case R.id.item3: Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show(); return true; default: return super.onOptionsItemSelected(item); } } } Đầu ra: Sản lượng mà không cần nhấp vào nút menu. Đầu ra sau khi nhấp vào nút menu. Đầu ra sau khi nhấp vào mục trình đơn thứ hai. Option Menu với Icon Bạn cần phải có những hình ảnh biểu tượng bên trong thư mục res/drawable. Android:icon được sử dụng để hiển thị các biểu tượng trên menu tùy chọn. Bạn có thể viết các thông tin chuỗi trong file strings.xml. Nhưng chúng ta đã viết nó bên trong các tập tin menu_main.xml. File: menu_main.xml Code: package com.javatpoint.optionmenu; 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. getMenuInflater().inflate(R.menu.main, menu);//Menu Resource, Menu return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.item1: Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show(); return true; case R.id.item2: Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show(); return true; case R.id.item3: Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show(); return true; default: return super.onOptionsItemSelected(item); } } } ----------------- || ----------------- Trung tâm Đào tạo học lập trình php cơ bản tại hà nội !