简体   繁体   English

Android将项目添加到全局上下文菜单

[英]Android add item to global context menu

When you long press on something in Android, a context menu comes up. 当您在Android中长按某些内容时,会出现一个上下文菜单。 I want to add something to this context menu for all TextViews in the system. 我想为系统中所有TextViews上下文菜单添加一些内容。

For example, the system does this with Copy and Paste. 例如,系统使用“复制”和“粘贴”执行此操作。 I would want to add my own, and have it appear in every application. 我想添加自己的,并让它出现在每个应用程序中。

Currently Android does not support this, you cannot override or hook functionality globally at the system level without the particular activity implementing an intent or activity that you expose. 目前Android不支持此功能,如果没有特定活动实现您公开的意图或活动,则无法在系统级别全局覆盖或挂钩功能。 Even in the case of publishing an intent it wouldn't matter unless the application running is a consumer... and all the base system applications and obviously all applications prior to yours would not be without updating the app to consume. 即使在发布意图的情况下,除非运行的应用程序是消费者,否则无关紧要......并且所有基本系统应用程序以及显然所有应用程序之前的应用程序都不会没有更新要使用的应用程序。

Basically as it stands, this is not possible. 基本上看来,这是不可能的。

What exactly are you trying to accomplish with this global context menu, some sort of global "Search For" or "Send To" functionality that runs through your application? 您正在尝试使用此全局上下文菜单,某种通过您的应用程序运行的某种全局“搜索”或“发送到”功能来完成什么?

Push the Share button (tree of three dots) within selection menu. 在选择菜单中按“共享”按钮(三个点的树)。 Then you should select your own app. 然后你应该选择自己的应用程序。 Does't work for input field content, unfortunately. 遗憾的是,它不适用于输入字段内容。

Add intent-filter in your file android-manifest.xml: 在文件android-manifest.xml中添加intent-filter:

<activity
   android:name=".ProcessTextActivity"
   android:label="@string/process_text_action_name">
  <intent-filter>
     <action android:name="android.intent.action.PROCESS_TEXT" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:mimeType="text/plain" />
 </intent-filter>
</activity>

Get highlighted by user text in activity your app in method onCreate(): 通过方法onCreate()中的应用活动中的用户文本突出显示:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.process_text_main);
    CharSequence text = getIntent()
      .getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
    // process the text
}

More information in the article on medium . 关于媒体的文章中的更多信息。

Thanks for user: YungBlade 感谢用户: YungBlade

Him answer on ru.stackoverflow.com 他在ru.stackoverflow.com回答

Hmm; 嗯; I don't know if you can extend built in types like in eg. 我不知道你是否可以扩展内置类型,例如。 Ruby (my Java knowledge is not so big). Ruby(我的Java知识不是那么大)。

However you can derive your own MyTextView from TextView. 但是,您可以从TextView派生自己的MyTextView。 Then substitute all your TextViews in layouts like this: 然后在布局中替换所有TextView,如下所示:

<TextView android:id="@+id/TextView01" />

to

<com.mydomain.mypackage.MyTextView android:id="@+id/TextView01" />

to automatically change type of all these fields. 自动更改所有这些字段的类型。

Then you need to override all constructors (especially TextView(Context context, AttributeSet attrs) ). 然后,您需要覆盖所有构造函数(尤其是TextView(Context context, AttributeSet attrs) )。

After that all layout inflaters (automatic or manual) will create this type with no fuss. 之后,所有布局的inflaters(自动或手动)将创建这种类型,没有大惊小怪。

Then you can create/register context menu callbacks as you wish. 然后,您可以根据需要创建/注册上下文菜单回调。

This might be a little bit hacky, but you can trap the menu key at the application/activity level, check to see if your current active view is a text entry view, and then build and display your own custom popup menu. 这可能有点hacky,但您可以在应用程序/活动级别捕获菜单键,检查当前活动视图是否是文本输入视图,然后构建并显示您自己的自定义弹出菜单。

It is possible, it's just a little tricky. 有可能,这有点棘手。

If you create/inflate a TextView, call setFocusable(false) on it, and then set it as the active view your Activity will still receive key events. 如果您创建/膨胀TextView,请在其上调用setFocusable(false),然后将其设置为活动视图,您的Activity仍将接收关键事件。 You will have to forward all those events (trackball, touch, key) to your View tree by hand. 您必须手动将所有这些事件(轨迹球,触摸,键)转发到您的View树。 (Inside your "onKeyDown" function you'd have to call the appropriate "onKeyDown" method for the top level View) You effectively have to trap the notion of 'focus' and dole it out to the correct view yourself. (在你的“onKeyDown”函数中,你必须为顶级视图调用适当的“onKeyDown”方法)你有效地必须捕获“焦点”的概念并将其自己发送到正确的视图。

While a little ugly, it may give you the desired results. 虽然有点难看,它可能会给你想要的结果。

This would, however, only work in your own application. 但是,这只适用于您自己的应用程序。 Previous answers are correct about it being impossible across the entire phone. 以前的答案是正确的,在整个手机上是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM