微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 文本选择弹出窗口?

我想在用户选择EditText中的文本时显示弹出窗口.

这是我想要做的:(来自Google Docs应用程序的屏幕截图)

我还想在该弹出窗口中添加自定义操作.

有关如何实现这一点的任何想法?

编辑:我特别想要一个浮动弹出窗口,就像屏幕截图中显示的那样,而不是ActionMode,因为appb中显示了有价值的信息.

我知道我可以让动作模式按下它下面的屏幕内容.但我真的需要一个文本选择弹出窗口.

解决方法:

让我们看看如果为给定的textView设置textIsSelectable属性为true,您将获得的认行为.

在TextView上的onLongPress你会得到这个

enter image description here


根据android的API级别,事情可能会有所不同,

现在让我们定制一些东西,

我假设您将使用支持库v7,因此为ToolBar制作了东西.

确保您的应用程序主题具有包含此元素的样式.

    <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

contextual_action_mode_test_one_activity.xml

<?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>

ContextualActionModeTestOneActivity.java

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) {

        }
    }
}

长按textView这就是你会看到的

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐