日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android Contextual Menus之二:contextual action mode

發(fā)布時間:2025/5/22 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Contextual Menus之二:contextual action mode 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

Android Contextual Menus之二:contextual action mode

?

  接上文:Android Contextual Menus之一:floating context menu

  ContextMenu的兩種形式,上文討論了第一種形式,兼容性較好。

  本文討論第二種形式,Android 3.0,即API Level 11之后可用。

?

Contextual action mode

  Contextual action mode是?ActionMode?的系統(tǒng)實現(xiàn),關(guān)注于執(zhí)行上下文相關(guān)動作的用戶交互。

  當用戶通過選擇一個項目使能這個模式,一個contextual action bar就會出現(xiàn)在屏幕上方,顯示用戶對當前選中的項目可以執(zhí)行的動作。

  當這個模式使能時,用戶可以:選擇多個項目(如果你允許的話)、取消項目選擇、在activity中繼續(xù)瀏覽(只要你允許)。

  當用戶取消對所有項目的選擇、按下Back鍵、或者點擊bar左邊的完成按鈕之后,action mode就被禁用,contextual action bar消失。

  注意:contextual action bar沒有必須?action bar關(guān)聯(lián),它們是獨立的。

?

CAB的使用情形

  對于提供上下文動作的View,通常在這兩種情況下(情況之一或both)調(diào)用contextual action mode

  1.用戶在View上長按;

  2.用戶選擇了View中的CheckBox或者類似控件。

?

  你的應(yīng)用如何invoke這個contextual action mode,以及如何定義每個action取決于你自己的設(shè)計。

  兩種基本的設(shè)計:

  1.對個體任意views的上下文相關(guān)操作;

  For contextual actions on individual, arbitrary views.

  2.對一組數(shù)據(jù)的批處理,比如ListView或GridView中的項目,允許用戶選擇多個項目然后對它們整體執(zhí)行一個動作。

  For batch contextual actions on groups of items in a ListView or GridView (allowing the user to select multiple items and perform an action on them all).

?

  下面分別講講這兩種情景下的實現(xiàn)。

?

Enabling the contextual action mode for individual views

  如果你想在用戶選擇指定View的時候invoke contextual action mode(CAB),你應(yīng)該:

  1.實現(xiàn)ActionMode.Callback接口。

  在這個接口的回調(diào)方法中,你可以指定contextual action bar的動作,響應(yīng)action items的點擊事件,還有處理action mode的生命周期事件。

  2.當你想要show這個bar的時候(比如用戶長按view的時候),調(diào)用?startActionMode()方法。

  例子代碼:

package com.example.mengdd.hellocontextmenu;import android.app.Activity; import android.os.Bundle; import android.view.ActionMode; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnLongClickListener; import android.widget.TextView; import android.widget.Toast;public class ContextualActionModeActivity extends Activity {private TextView mTextView = null;private ActionMode mActionMode = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_contextual_action_mode);mTextView = (TextView) findViewById(R.id.textView2);mTextView.setOnLongClickListener(new OnLongClickListener() {@Overridepublic boolean onLongClick(View view) {if (mActionMode != null) {return false;}// Start the CAB using the ActionMode.Callback defined abovemActionMode = startActionMode(mActionModeCallback);view.setSelected(true);return true;}});}private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {// Called when the action mode is created; startActionMode() was called @Overridepublic boolean onCreateActionMode(ActionMode mode, Menu menu) {// Inflate a menu resource providing context menu itemsMenuInflater inflater = mode.getMenuInflater();inflater.inflate(R.menu.context_menu1, menu);return true;}// Called each time the action mode is shown. Always called after// onCreateActionMode, but// may be called multiple times if the mode is invalidated. @Overridepublic boolean onPrepareActionMode(ActionMode mode, Menu menu) {return false; // Return false if nothing is done }// Called when the user selects a contextual menu item @Overridepublic boolean onActionItemClicked(ActionMode mode, MenuItem item) {switch (item.getItemId()) {case R.id.edit:showEditor();mode.finish(); // Action picked, so close the CABreturn true;default:return false;}}// Called when the user exits the action mode @Overridepublic void onDestroyActionMode(ActionMode mode) {mActionMode = null;}};private void showEditor() {Toast.makeText(ContextualActionModeActivity.this, "edit",Toast.LENGTH_LONG).show();} } CAB Demo1

?

?

Enabling batch contextual actions in a ListView or GridView

  對于ListView和GridView這樣的集合類,想讓用戶進行批處理操作,應(yīng)該如下:

  1.實現(xiàn)?AbsListView.MultiChoiceModeListener接口,通過setMultiChoiceModeListener()方法把它set進集合類控件。

  在這個listener的回調(diào)方法中,你可以指定contextual action bar的動作,響應(yīng)action item的點擊事件,處理其他繼承自ActionMode.Callback的回調(diào)。

  2.調(diào)用?setChoiceMode()方法,使用參數(shù)?CHOICE_MODE_MULTIPLE_MODAL?。

  例子代碼:

package com.example.mengdd.hellocontextmenu;import android.app.ListActivity; import android.os.Bundle; import android.view.ActionMode; import android.view.Menu; import android.view.MenuItem; import android.view.MenuInflater; import android.widget.AbsListView.MultiChoiceModeListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast;public class ListCABActivity extends ListActivity {private ListView mListView = null;private String[] mStrings = Cheeses.sCheeseStrings;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Use an existing ListAdapter that will map an array// of strings to TextViewssetListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mStrings));mListView = getListView();mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {@Overridepublic void onItemCheckedStateChanged(ActionMode mode,int position, long id, boolean checked) {// Here you can do something when items are// selected/de-selected,// such as update the title in the CAB }@Overridepublic boolean onActionItemClicked(ActionMode mode, MenuItem item) {// Respond to clicks on the actions in the CABswitch (item.getItemId()) {case R.id.delete:deleteSelectedItems();mode.finish(); // Action picked, so close the CABreturn true;default:return false;}}@Overridepublic boolean onCreateActionMode(ActionMode mode, Menu menu) {// Inflate the menu for the CABMenuInflater inflater = mode.getMenuInflater();inflater.inflate(R.menu.context_menu2, menu);return true;}@Overridepublic void onDestroyActionMode(ActionMode mode) {// Here you can make any necessary updates to the activity when// the CAB is removed. By default, selected items are// deselected/unchecked. }@Overridepublic boolean onPrepareActionMode(ActionMode mode, Menu menu) {// Here you can perform updates to the CAB due to// an invalidate() requestreturn false;}});}private void deleteSelectedItems() {Toast.makeText(ListCABActivity.this, "delete!", Toast.LENGTH_LONG).show();} } CAB Demo2

?

參考資料

  API Guides: Menus->Using the contextual action mode

  http://developer.android.com/guide/topics/ui/menus.html#CAB

?

轉(zhuǎn)載于:https://www.cnblogs.com/mengdd/p/3565213.html

總結(jié)

以上是生活随笔為你收集整理的Android Contextual Menus之二:contextual action mode的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。