Android 长按编辑文本行为

发布于 2024-10-02 00:38:48 字数 274 浏览 5 评论 0原文

是否可以将某些内容添加到用户长按任何编辑文本时显示的项目列表中? (剪切、复制粘贴、选择文本、全选、输入法)我想为此添加一个选项,该选项将扫描二维码并将结果粘贴到编辑文本中。我认为从我放入自己的应用程序中的编辑文本中删除此行为并不难,但我想将此选项添加到手机上任何应用程序内的任何编辑文本中。这样的事情可能吗,如果可以的话,有人能指出我正确的方向吗?

编辑 150 赏金:我希望在长按时将一个项目添加到 EditText 弹出对话框中。我想寻找一种方法来在整个系统范围内进行此更改,而不仅仅是在一个应用程序的上下文中。

Is it possible to add something to the list of items that shows up when a user long presses on any Edit Text? (Cut, copy paste, select text, select all, input method) I want to add an option to this that will scan a QR code and paste the result into the Edit Text. I think this would not be very hard to get this behavior out of Edit Texts that I put into my own application, but I am wanting to add this option to any Edit Text inside any application on my phone. Is something like this possible, if so can anyone point me in the right direction?

Edit 150 bounty: I am looking to add an item to the EditText pop-up dialog when it is long pressed. I want am looking for a way to make this change system wide, not just within the context of 1 application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所谓喜欢 2024-10-09 00:38:48

这是不可能的,因为上下文菜单是由应用程序本身而不是系统填充的。您不能强制其他应用程序拥有它们在其生命周期中可能不会使用的上下文项。您至少可以在识别您的应用程序的应用程序中拥有该功能。

创建一个仅填充和处理全局菜单项的活动。其他应用程序可以通过扩展您的活动来使用该功能。但这也会产生问题,因为其他应用程序将严重依赖您的应用程序。因此,如果您的应用程序未安装在该系统中,则其他应用程序将无法运行。此外,无法在清单文件中指示此依赖性,因此如果您的应用程序尚未安装,则依赖的应用程序会隐藏在市场中。

我确信这不是您正在寻找的答案,但上下文菜单是经过设计的。

Thats not possible, as the context menu is populated by applications themselves and not by the system. You cannot force other apps to have a context item that they might not use in their lifetime. You can atleast have the feature in apps that are aware of your app.

Create an activity that populates and handles only your global menu items. Other apps can use the feature by extending your activity. But this too will create problems, as the other apps will have a hard dependency on your app. So if your app is not installed in that system then the other app won't work. Also there is no way to indicate of this dependency in the manifest file so that the dependent app is hidden in the market if your app is not already installed.

I'm sure this is not the answer you were looking for, but context menus are made so by design.

丑疤怪 2024-10-09 00:38:48

有两种方法:Shahab 描述的第一种方法。第二个更简单。您只需覆盖活动的标准方法,例如:

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)
{
       if(view.getId()==R.id.MyEditTextId)
       {
            menu.add(Menu.NONE, MyMenu, Menu.NONE, R.string.MyMenuText);
       }
       else
          super.onCreateContextMenu(menu, view, menuInfo);
}

之后您将长按弹出上下文菜单

There are 2 ways: 1st one described by Shahab. 2nd one is more simple. You need to just override standard method of your activity, like:

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)
{
       if(view.getId()==R.id.MyEditTextId)
       {
            menu.add(Menu.NONE, MyMenu, Menu.NONE, R.string.MyMenuText);
       }
       else
          super.onCreateContextMenu(menu, view, menuInfo);
}

After that you'll have on long press popup context menu

深海少女心 2024-10-09 00:38:48

是的,可以在 EditText 的 LongClick 上将某些内容添加到项目列表中。

为了让您找到正确的方向,我发布了一些代码片段。

在 main.xml 中执行类似的操作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
>

<EditText  
              android:id="@+id/textt"
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:text="@string/hello"
/>

</LinearLayout>

之后,在主 Activity 中执行类似的操作

public class edit extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText text = (EditText)this.findViewById(R.id.textt);
    text.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            //ADD HERE ABOUT CUT COPY PASTE  
            // TODO Auto-generated method stub
            return false;
        }
    });
}
}

希望有帮助

Yes it is possible to add something to the list of items on LongClick of EditText.

To put you in right direction I am posting some code snippets.

In main.xml do something like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
>

<EditText  
              android:id="@+id/textt"
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:text="@string/hello"
/>

</LinearLayout>

After that in your main Activity, do something like

public class edit extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText text = (EditText)this.findViewById(R.id.textt);
    text.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            //ADD HERE ABOUT CUT COPY PASTE  
            // TODO Auto-generated method stub
            return false;
        }
    });
}
}

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文