文字选择弹出窗口?

时间:2016-01-30 03:07:36

标签: android

每当用户选择EditText中的文字时,我都想显示一个弹出窗口。

以下是我要做的事情:(来自Google文档应用的屏幕截图)

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

关于如何实现这一目标的任何想法?

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

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

1 个答案:

答案 0 :(得分:0)

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

onLongPress上的{p> TextView你会得到这个 enter image description here

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

现在让我们自定义一些东西,

我假设您将使用support library 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