2

我有一个非常烦人的问题。我正在开发一个应用程序,现在我需要获取 TextView 中的文本并将其传递到剪贴板。换句话说,我需要复制文本。

android:textIsSelectable = "true"适用于新版本,但我需要这个应用程序在 API10 ( 2.3.3 ) 上运行

我试过这个:

    import android.text.ClipboardManager;
    [ . . . ]
    private CharSequence code;
    [ . . . ]
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {

        codeTextView.setOnLongClickListener(new OnLongClickListener() {

            public boolean onLongClick(View v) {
                code = codeTextView.getText();
                ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
                clipboard.setText(code);
                Log.i(TAG, "COPIED! ->" + (clipboard.getText()));

                return false;
            }
        });

好的。问题是:CLIPBOARD_SERVICE有一个错误: CLIPBOARD_SERVICE cannot be resolved to a variable

如何摆脱这个?我的意思是,如果我尝试删除它,似乎“getSystemService”方法不存在。这是怎么回事?

笔记:

  • 我正在使用 appcompat_v7
  • 在 Honeycomb 及更高版本上正常运行
  • 版本检查工作正常
4

1 回答 1

3

简单的:

使用Context.CLIPBOARD_SERVICE

ClipboardManager clipboard = (android.text.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE); 

CLIPBOARD_SERVICE是 的静态场Context。我猜代码是在Context你得到它的地方的子类中使用的,因为你的不是 的子类Context,你必须把它放在Context前面。

于 2014-09-25T17:43:34.803 回答