我将创建一个应用程序,该用户将输入我的应用程序的网站网址。然后,我使用WebView在我的应用程序中显示此页面。
如您所知,当用户单击上下文中的任意文本稍长时,将出现android光标,然后我们可以根据需要选择任意数量的文本。
选择后,我们将看到“复制,共享,全选”等。
我的问题是,当用户选择文本时,我想给他们显示不同的选项。假设“ MyCOPY,SendTwitter,SendMessage”。
我该怎么做?
到目前为止我做了什么?
代码如下:
private WebView view;
private final String TAG = MainActivity.class.getSimpleName();
private ActionMode actionMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.view = findViewById(R.id.webView);
view.loadUrl("https://stackoverflow.com/questions/28385768/android-how-to-check-for-successful-load-of-url-when-using-webview-loadurl");
view.setWebViewClient(new MyWebViewClient());
Log.d(TAG, view.getUrl());
view.setOnLongClickListener((v) -> {
if (actionMode != null)
return false;
actionMode = startSupportActionMode(actionCallBack);
return true;
});
}
startSupportActionMode(actionCallBack)在哪里
private ActionMode.Callback actionCallBack = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.custommenu, menu);
actionMode.setTitle("Choose");
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.example_item_1:
Toast.makeText(MainActivity.this, "Option 1 selected", Toast.LENGTH_SHORT).show();
actionMode.finish();
return true;
case R.id.example_item_2 :
Toast.makeText(MainActivity.this, "Option 2 selected", Toast.LENGTH_SHORT).show();
actionMode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
actionMode = null;
}
};
答案 0 :(得分:0)
您可以实施ActionMode.Callback界面以在选择时创建自己的菜单。
动作模式的生命周期如下:
onCreateActionMode(ActionMode,Menu)一次创建一次
onPrepareActionMode(ActionMode,菜单)在创建后以及任何时候 ActionMode无效 onActionItemClicked(ActionMode,MenuItem) 任何时候单击上下文操作按钮 动作模式关闭时onDestroyActionMode(ActionMode)
只需确保您的文本视图允许选择文本(android:textIsSelectable="true"
)
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, 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.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_share:
shareCurrentItem();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
然后在适当的时候(source)(例如在setOnLongClickListener内部)调用startActionMode()
以启用上下文操作模式。