我見過一堆應用程序,在ListView上按住一個條目(比點擊更長)會產生一個彈出窗口。它通常列出了對條目採取的操作(編輯,刪除等)。如何在Android上實現彈出窗口?
這是內置於Android的東西或我必須建立自己的東西?
我見過一堆應用程序,在ListView上按住一個條目(比點擊更長)會產生一個彈出窗口。它通常列出了對條目採取的操作(編輯,刪除等)。如何在Android上實現彈出窗口?
這是內置於Android的東西或我必須建立自己的東西?
您可以創建一個對話框(有很多關於Android對話框的文檔,您可以從這裏開始http://developer.android.com/guide/topics/ui/dialogs.html)。或者,您可以開始另一項活動,這對我來說似乎更適合Android,儘管它可能只是一種感覺。
就像之前的海報說的那樣,你可以創建一個對話框,並且你提到的長按可以使用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所示)結合起來用於您的實際上下文菜單,則您已獲得所需的一切。實現示例非常普遍。
只是爲了澄清,你的意思是[這個UI模式](http://www.androidpatterns.com/uap_pattern/quick-actions)? – 2011-12-23 20:13:24
@alextsc我不需要它那麼花哨,但是,這足夠接近。 – AngryHacker 2011-12-23 20:59:49