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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…