Selecting text on TextView (android 2.2)

大城市里の小女人 提交于 2019-11-28 06:05:28

问题


How to implement selecting text capability on Android 2.2? I searched Google but can't find a solution.


回答1:


This is the only way I've found (from google) to support it for android 1.6+, it works but it's not ideal, I think in stock android you can't hold down a webview to highlight until v2.3, but I may be mistaken..

By the way this is for a webview, it might also work on textview, but I haven't tried it

(Note: this is currently what my shipped app is using, so it works with all the phones I've had it tested on, but the only reason I found this question was because I was searching and hoping that someone had come up with a better way by now)

I've just got a menu item called "Select text" which calls the function "selectnCopy()"

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //..
    switch (item.getItemId()) {
        case R.id.menu_select:
            selectnCopy();
            return true;
    //..
    }
}

Which looks like this:

public void selectnCopy() {
    Toast.makeText(WebClass.this,getString(R.string.select_help),Toast.LENGTH_SHORT).show();
    try {
        KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
        shiftPressEvent.dispatch(wv);
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

Notice I've put the select_help string there as a toast, that's just because it's not immediately clear to the user how it's supposed to work

<string name="select_help">Touch and drag to copy text.</string>



回答2:


The Link @Stefan Hållén provided only worked after API Level 11.

And Android 2.2 is API Lv.8, that's the reason why you cannot get a resource identifier.




回答3:


Have you set the text to be selectable? like this:

android:textIsSelectable="false"  //OR true

See the Documentation for further reference.




回答4:


After a long and time consuming search, I can't find a component that can select text in textview for android API level <=11. I have written this component that may be of help to you : new Selectable TextView in android 3 (API <=11) component




回答5:


An interesting workaround:

You could try displaying your text in a webview.

You just have to write the HTML tags and all of that into your string to display, and it should be selectable using the WebKit browser.

This should be fairly lightweight and transparent to the user, and I think it would solve your problem.

Let me know if you need a code example, it should be fairly simple. Just check out the WebView docs on http://developer.android.com/resources/tutorials/views/hello-webview.html

Best of luck!



来源:https://stackoverflow.com/questions/6625300/selecting-text-on-textview-android-2-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!