new Selectable TextView in android 3 (API <=11) component

大憨熊 提交于 2019-11-28 10:09:25
vamsiampolu

Also add the following two constructors because of this

    public SelectableTextView(Context context,AttributeSet attrs)
{
    super(context,attrs);
}

public SelectableTextView(Context context,AttributeSet attrs,int defStyle)
{
    super(context,attrs,defStyle);
}

I have also added the following to the code:

    @SuppressLint("NewApi")
public void setTextIsSelectable(boolean selectable) {
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
        super.setTextIsSelectable(true);
    else
    {   
        super.setLongClickable(true);
        super.setOnLongClickListener(getSelectableLongClick());
        super.setOnTouchListener(getSelectableOnTouch());
    }
}

I used it like this,without an OnTouchListener:

txt_copyFrom.setClickable(false);
txt_copyFrom.setCursorVisible(false);
txt_copyFrom.setEnabled(true);
txt_copyFrom.setTextIsSelectable(true);
txt_copyFrom.setOnLongClickListener(new OnLongClickListener(){

    @Override
    public boolean onLongClick(View v) {
        int start=txt_copyFrom.getSelectionStart();
        int end=txt_copyFrom.getSelectionEnd();

        mSelectedText=txt_copyFrom.getText().toString().substring(start, end);
        Log.d(TAG, "Selected text: "+mSelectedText);
        return true;
    }

});

with the XML:

<com.example.clipboardtest.SelectableTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Old Buccaneer1The Old Sea-dog at the Admiral BenbowSquire Trelawney, Dr. Livesey, and the rest of these gentlemen having asked me to write down the whole particulars about Treasure Island, from the beginning to the end, keeping nothing back but the bearings of the island, and that only because there is still treasure not yet lifted, I take up my pen in the year of grace 17--and go back to the time when my father kept the Admiral Benbow inn and the brown old seaman with the sabre cut first took up his lodging under our roof."
    android:id="@+id/txt_copyfrom"
/>         

I guess I have to set an OnTouchListener within the OnLongClickListener for the TextView in the Activity itself.

Tried putting Logs all over the place in SelectableTextView,it does not seem to be working...I find that the LongClickListener is called but the TouchListener is not even called...

Setting the OnTouchListener inside the OnLongClickListener did this

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