Android将项目添加到全局上下文菜单[英] Android add item to global context menu

本文是小编为大家收集整理的关于Android将项目添加到全局上下文菜单的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

当您长时间按Android中的某些内容时,会出现上下文菜单.我想在系统中的所有TextViews中添加一些上下文菜单.

例如,系统使用复制和粘贴来完成此操作.我想添加自己的,并在每个应用程序中出现.

推荐答案

当前Android不支持这一点,如果没有特定的活动实施您所公开的意图或活动,则无法在系统级别上覆盖或挂钩功能.即使在发布意图的情况下,除非运行的应用程序是消费者...并且所有基本系统应用程序以及您之前的所有申请都不会在您的所有基本系统应用程序中不使用更新应用程序以消费.

.

基本上,这是不可能的.

您到底要使用此全局上下文菜单,某种全局的"搜索"或"发送到"功能?

其他推荐答案

文件中添加intent-filter android-manifest.xml:

<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>

在Activity您的应用程序中,用户文本在Method otCreate():

中都会突出显示.
  @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
}

中等.

感谢用户: yungblade

他回答 ru.stackoverflow.com

其他推荐答案

嗯;我不知道您是否可以扩展以例如例如示例类型的类型. Ruby(我的Java知识还不够深).

但是,您可以从TextView得出自己的MyTextView.然后在这样的布局中替换所有文本视图:

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

to

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

自动更改所有这些字段的类型.

然后您需要覆盖所有构造函数 (特别是TextView(Context context, AttributeSet attrs)).

之后,所有布局充气器(自动或手动)都不会大惊小怪.

然后您可以根据需要创建/注册上下文菜单回调.

本文地址:https://www.itbaoku.cn/post/102509.html

问题描述

When you long press on something in Android, a context menu comes up. I want to add something to this context menu for all TextViews in the system.

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. 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?

其他推荐答案

Add intent-filter in your file android-manifest.xml:

<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():

  @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

Him answer on ru.stackoverflow.com

其他推荐答案

Hmm; I don't know if you can extend built in types like in example. Ruby (my Java knowledge is not so deep enough).

However you can derive your own MyTextView from TextView. Then substitute all your TextView in layouts like this:

<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) ).

After that all layout inflaters (automatic or manual) will create this type with no fuss.

Then you can create/register context menu callbacks as you wish.