簡體   English   中英

如何在Android上實現彈出窗口?

[英]How to implement a popup on Android?

我見過很多應用程序,在其中按住ListView上的某個條目一段時間(長於單擊)會彈出一個窗口。 它通常列出要對該條目執行的操作(編輯,刪除等)。

這是Android內置的東西還是我必須自己構建的東西?

您可以創建一個Dialog(關於Android對話框的文檔很多,您可以從http://developer.android.com/guide/topics/ui/dialogs.html開始)。 或者,您可以啟動另一個活動,對我來說,這似乎更適合Android,盡管可能只是一種感覺。

就像上一個海報所說的那樣,您可以創建一個Dialog,而您提到的長按可以使用setOnItemLongClickListener來實現。 祝好運!

(將偵聽器從longclick修改為itemlongclick)

保持條目較長時間將觸發上下文菜單。

在onCreate中使用它:

registerForContextMenu(getListView());

然后覆蓋:

@Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();
        int idcompte = mComptes.get(info.position).getId();

        switch (item.getItemId()) {
            case DELETE_ID:
                DBhelper dBhelper = new DBhelper(this);
                dBhelper.open();
                dBhelper.deleteCompte(idcompte);
                dBhelper.close();
                onResume();
                return true;

            case EDIT_ID:

                Intent intent = new Intent(this, AddorupdateCompteActivity.class);
                intent.putExtra(AddorupdateCompteActivity.ID, idcompte);
                startActivity(intent);

                return true;
        }
        return super.onContextItemSelected(item);
    }

以上建議對我而言似乎不是最直接的方法。 就像ListView具有setOnItemSelectedListener ,長按等效項也稱為setOnItemLongClickListener

如果將此偵聽器與onContextItemSelected (如Noureddine AMRI所示)結合起來用於實際的上下文菜單,則您將擁有所需的一切。 實現示例很普遍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM