Computer >> Máy Tính >  >> Lập trình >> Android

Ví dụ về menu cửa sổ bật lên của Android

Menu bật lên giống như một menu, nó sẽ được hiển thị ở trên hoặc dưới chế độ xem tùy theo không gian hoạt động. Đây là giải pháp đơn giản để tạo menu bật lên android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:tools = "https://schemas.android.com/tools"
   android:id = "@+id/rootview"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical"
   android:background = "#c1c1c1"
   android:gravity = "center_horizontal"
   tools:context = ".MainActivity">
   <Button
      android:id = "@+id/popup"
      android:text = "Download"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã đưa ra nút. khi bạn nhấp vào nút trên, nó sẽ hiển thị menu bật lên.

Bước 3 - Thêm mã sau vào src / MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   Button popupButton;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      popupButton = findViewById(R.id.popup);
      popupButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            popupMenuExample();
         }
      });
   }
   private void popupMenuExample() {
      PopupMenu p = new PopupMenu(MainActivity.this, popupButton);
      p.getMenuInflater().inflate(R.menu.popup_menu_example, p .getMenu());
      p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
         public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(MainActivity.this,item.getTitle(), Toast.LENGTH_SHORT).show();
            return true;
         }
      });
      p.show();
   }
}

Trong đoạn mã trên khi bạn nhấp vào nút, nó sẽ tạo đối tượng menu bật lên và thêm vào menu như hình dưới đây -

PopupMenu p = new PopupMenu(MainActivity.this, popupButton);
p.getMenuInflater().inflate(R.menu.popup_menu_example, p .getMenu());

Trong đoạn mã trên, chúng tôi có menu tăng cao dưới dạng popup_menu_example như được hiển thị bên dưới -

<?xml version = "1.0" encoding = "utf-8"?>
<menu xmlns:android = "https://schemas.android.com/apk/res/android">
   <item
      android:id = "@+id/android"
      android:title = "Android" />
   <item
      android:id = "@+id/java"
      android:title = "JAVA"/>
   <item
      android:id = "@+id/kotlin"
      android:title = "Kotlin"/>
</menu>

Khi người dùng nhấp vào mục menu, nó sẽ gọi onMenuItemClickListener () như hình dưới đây -

p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
   public boolean onMenuItemClick(MenuItem item) {
      Toast.makeText(MainActivity.this,item.getTitle(), Toast.LENGTH_SHORT).show();
      return true;
   }
});

Để hiển thị menu bật lên, chúng ta phải gọi show () như hình dưới đây -

p.show();

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Ví dụ về menu cửa sổ bật lên của Android

Khi bạn nhấp vào nút tải xuống, nó sẽ hiển thị menu bật lên. Nút không có khoảng trống ở trên nên nó sẽ hiển thị ở dưới cùng. như hình bên dưới -

Ví dụ về menu cửa sổ bật lên của Android

Bây giờ bấm vào bất kỳ mục nào nó sẽ cho thông báo như hình bên dưới -

Ví dụ về menu cửa sổ bật lên của Android