在webview上创建自定义菜单

时间:2013-12-24 08:58:29

标签: android webview android-webview

如长按事件发生时如何为webview创建自定义菜单,如顶部From playbooks application中的图像所示?

 public class MainActivity extends Activity {

private String data;
private WebView webview;
private String clipdata = "";
private boolean mark_text;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview = (WebView) this.findViewById(R.id.webView1);

    data = "The entire plan-to-produce process, including enterprise-level planning and scheduling, plant-level operations, manufacturing execution, batch manufacturing, and quality management. Capabilities for Big Data management and process integration support the use of real-time data from the shop floor to maintain batch traceability and genealogy. Embedded quality and compliance controls enable process manufacturers to manage exceptions and address nonconformance through corrective and preventive actions for batches. Leveraging mobile and cloud as well ![enter image description here][2]as on-premise technologies, this level of production control helps increase throughput, set predictable and shorter cycle times, improve asset utilization, and help ensure that inventory targets are met.</body></html>";
    webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case R.id.action_search:
        Toast.makeText(this, "action_search selected", Toast.LENGTH_SHORT)
                .show();
        return true;
    case R.id.action_settings:
        Toast.makeText(this, "action_settings selected", Toast.LENGTH_SHORT)
                .show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

}

enter image description here

我的代码更新了答案。请检查它并说明如何禁用此默认操作栏并添加我自己的操作栏,但同时文本选择功能应该有效。

3 个答案:

答案 0 :(得分:1)

这称为Contextual Action Bar,它是默认操作栏顶部的叠加层。 有一个很好的教程here,它描述了如何使用它。

要让自己的行动吧在那里工作,同时仍然保持选择功能,至少可以说我害怕......

答案 1 :(得分:0)

要向操作栏添加操作,请在项目的res/menu/目录中创建新的XML文件。

main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:showAsAction="never" />
</menu>

将操作添加到操作栏

要将菜单项放入操作栏,请在活动中实施onCreateOptionsMenu()回调方法,以将菜单资源扩展为给定的Menu对象。例如:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}

回应行动按钮

当用户按下动作溢出中的某个动作按钮或其他项目时,系统会调用您的活动的onOptionsItemSelected()回调方法。在此方法的实现中,在给定的MenuItem上调用getItemId()以确定按下了哪个项 - 返回的ID与您在相应元素的android:id属性中声明的值匹配。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
             Toast.makeText(this,"action_search selected",Toast.LENGTH_SHORT).show();
            return true;
        case R.id.action_settings:
           Toast.makeText(this,"action_settings selected",Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.on

OptionsItemSelected(item);
    }
}

有关进一步指导,请参阅官方doc

答案 2 :(得分:0)

首先创建自己的ActionModeCallBack然后你需要创建一个类并扩展WebView并覆盖这个方法:

public ActionMode startActionMode(ActionMode.Callback callback) 
{
    actionModeCallback = new CustomizedSelectActionModeCallback();
    return super.startActionMode(actionModeCallback);
}    

更新:查看this
  这对我有用......