Android Long Press on Edit Text behavior

前端 未结 3 661
攒了一身酷
攒了一身酷 2020-12-05 21:44

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

相关标签:
3条回答
  • 2020-12-05 21:51

    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.

    0 讨论(0)
  • 2020-12-05 21:58

    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

    0 讨论(0)
  • 2020-12-05 22:11

    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

    0 讨论(0)
提交回复
热议问题