Edittext that make selection on long click but do not show context menu?

后端 未结 2 1058
广开言路
广开言路 2021-01-17 02:37

i want to provide custom handlers that cut,copy text.

Target on Longclick

  1. Context Menu Should not appear.
  2. Text could get selected with tracker
相关标签:
2条回答
  • 2021-01-17 02:53

    Finally I have sorted this out. This solution perfectly works for me. First of all, you need to create the context menu background as follows

    menuback.xml

    <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
          <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>
          </shape>
        </item>
      </selector>
    

    Then Create your Custom Menu as follows

    mymenu.xml

    <?xml version="1.0" encoding="utf-8"?>
      <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/m" android:title=" " ></item>
      </menu>
    

    Now add the following lines on your

    styles.xml

    <resources>
    
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="actionModeBackground">@drawable/menuback</item>
        <item name="actionBarSize">1dp</item>
        <item name="android:actionModeCloseDrawable">@android:color/transparent</item>
    
    </style>
    
    </resources>
    

    And Finally set elevation of the actionbar to 0 and implement the ActionMode.CallBack as follows

           EditText editText=findViewById(R.id.ed);
    
        getSupportActionBar().setElevation(0);
        editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                mode.getMenu().clear();
                MenuInflater inflater=mode.getMenuInflater();
                inflater.inflate(R.menu.mymenu,menu);
                return true;
            }
    
            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }
    
            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }
    
            @Override
            public void onDestroyActionMode(ActionMode mode) {
    
            }
        });
    

    Ta...da... Here is the Final Result

    0 讨论(0)
  • 2021-01-17 03:00

    you want to get rid of the ActionMode? and create your own??

    you can override this and get notified when the default is about to be displayed

      @Override
      public void onSupportActionModeStarted(ActionMode mode) {     
        super.onSupportActionModeStarted(mode);
        //you can add your custom ui here
        mode.setCustomView(....);// it takes a view
    }
    

    if you want to close it then you call mode.finish(); you can call it there to close if or finish it if you do not need it.

    when it is about to die it calls this onSupportActionModeFinished(ActionMode mode)` the methods i have stated are for the support libraries, hope you know that.

    The rest is cheese

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