使用code的android显示文本的复制/粘贴上下文菜单 [英] Show copy/paste context menu on text using code android

查看:259
本文介绍了使用code的android显示文本的复制/粘贴上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的EditText 和它的文本会从通过code选择。但我想,允许用户剪切/复制选定的文本。然而,剪切/复制上下文菜单中没有出现,直到文本用户长presses。但随后就失去了实际的选择。所以,我想,以显示上下文菜单的文本得到了code选择。

I have an EditText and its text gets selected from through code. But I want to allow users to cut/copy the selected text. However, the cut/copy context menu doesn't appear until user long presses on text. But then it loses the actual selection. So, I'm thinking to show the context menu as the text get selected by the code.

我想这里面onFocusChanged,但没有出现了。

I tried this inside onFocusChanged, but nothing appeared.

openContextMenu(EditText);

推荐答案

如果我跟着你的用例正确,你可以从上testedEditText注册的onFocusChangeListener打开上下文菜单。

IF I follow you usecase correctly, you can open context menu from the onFocusChangeListener registered on the testedEditText.

我prepared对于一些小的测试,这似乎是正确支持您的用例。
您需要openMenu在其上选择了的EditText的内容挂钩。

I prepared some small test for that which seems to be correctly supporting your usecase. You need to openMenu on the hooks which are selecting the content in the EditText.

public class Main extends Activity {

private EditText testedEditText;
private Button selectingButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    selectingButton = (Button) findViewById(R.id.button);
    testedEditText = (EditText) findViewById(R.id.textView);
    registerForContextMenu(testedEditText);

    selectingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            testedEditText.setSelection(6, 11);
            openContextMenu(testedEditText);
        }
    });
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.cmenu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.select_all:

            return true;
        case R.id.copy:
            //do something
            return true;
        case R.id.cut:
            //do something
            return true;

        case R.id.paste:
            //do something
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

}

古怪足够登记 testedEditText.requestFocus(),并设定 onFocusChangedListener 的EditText上是不够的。

Weirdly enough registering testedEditText.requestFocus(), and setting onFocusChangedListener for EditText was not enough.

有关工作的补充XML文件:
cmenu.xml

Additional xml files for reference: cmenu.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/select_all"
      android:title="select all"
      />
<item android:id="@+id/copy"
      android:title="copy"
      />
<item android:id="@+id/cut"
      android:title="cut"
      />
<item android:id="@+id/paste"
      android:title="paste"
      />
</menu>

main.xml中

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World, Initial Text..."
        android:layout_centerInParent="true"
        />

<Button android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="button"
        />
</RelativeLayout>

这篇关于使用code的android显示文本的复制/粘贴上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆