Android Long Press on Edit Text behavior

2019-01-14 18:41发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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