Android – text selection pop-up?

I want to display a pop-up window when the user selects text in EditText

This is what I want to do: (screenshot from Google Docs APP)

I also want to add custom actions to this pop - up window

Any ideas on how to achieve this?

Edit: I especially want a floating pop-up window, as shown in the screenshot, rather than actionmode, because valuable information is displayed in APPB

I know I can make action mode press the screen content below it. But I really need a text selection pop-up

resolvent:

Let's look at the default behavior you will get if you set the textisselectable property to true for a given textview

Onlongpress on textview and you'll get this

Things may be different depending on the API level of Android,

Now let's customize something,

I assume that you will use the support library V7, so I made something for the toolbar

Make sure your application theme has a style that contains this element

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="actionModeCloseDrawable">@drawable/ic_done_white_24dp</item> // this drawable can be changed depending on what you want

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/ContextualActionModeTestOneActivity_add"
        android:icon="@android:drawable/ic_input_add"
        android:title="Add"
        android:titleCondensed="Add"
        app:showAsAction="ifRoom|withText" />

    <item
        android:id="@+id/ContextualActionModeTestOneActivity_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        android:titleCondensed="Search"
        app:showAsAction="ifRoom|withText" />

    <item
        android:id="@+id/ContextualActionModeTestOneActivity_sort"
        android:icon="@android:drawable/ic_menu_sort_by_size"
        android:title="Sort"
        android:titleCondensed="Sort"
        app:showAsAction="ifRoom|withText" />
    <item
        android:id="@+id/ContextualActionModeTestOneActivity_help"
        android:icon="@android:drawable/ic_menu_help"
        android:title="Help"
        android:titleCondensed="Help"
        app:showAsAction="ifRoom|withText" />
</menu>

public class ContextualActionModeTestOneActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contextual_action_mode_test_one);
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_custom_toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initializeUI();
    }

    private void initializeUI() {
        textView = (TextView) findViewById(R.id.ContextualActionModeTestOneActivity_textView);
        textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                startActionMode(new ActionBarCallBack());
                return true;
            }
        });


    }


    class ActionBarCallBack implements android.view.ActionMode.Callback{

        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
            mode.setTitle("Do it");
            getMenuInflater().inflate(R.menu.contextual_action_mode_test_one_activity, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
            switch(item.getItemId()){
                case R.id.ContextualActionModeTestOneActivity_add:
                    Toast.makeText(getBaseContext(), "add this text somewhere ", Toast.LENGTH_LONG).show();
                    mode.finish();    // Automatically exists the action mode, when the user selects this action
                    break;
                case R.id.ContextualActionModeTestOneActivity_search:
                    Toast.makeText(getBaseContext(), "search this text ", Toast.LENGTH_LONG).show();
                    break;
                case R.id.ContextualActionModeTestOneActivity_sort:
                    Toast.makeText(getBaseContext(), "sort", Toast.LENGTH_LONG).show();
                    break;
                case R.id.ContextualActionModeTestOneActivity_help:
                    Toast.makeText(getBaseContext(), "help with this", Toast.LENGTH_LONG).show();
                    break;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(android.view.ActionMode mode) {

        }
    }
}

Long press textview. That's what you'll see

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>