由于某种原因,我无法选择TextView的文本。 作为概述:
1-我已经实现了onTouchListener,可以在其中检测到左右滑动。
2-我在scrollView中有TextView
3-我对TextView中的文本进行了一些处理,在其中定义了可单击的Span以及其他Span属性。
我尝试了以下方法:
1-设置
android:textIsSelectable="true"
android:focusableInTouchMode="true"
android:focusable="true"
2-在父版式上添加以下行:
android:descendantFocusability="blocksDescendants"
我的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:paddingLeft="20dp"
android:paddingTop="50dp"
android:paddingRight="20dp"
android:paddingBottom="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/next"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:scaleType="fitCenter"
android:src="@drawable/ic_navigate_before_black_24dp" />
<ImageButton
android:id="@+id/share"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:padding="9dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_share_black_24dp" />
<ImageButton
android:id="@+id/star"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:padding="9dp"
android:scaleType="fitCenter"
android:src="@drawable/star_border" />
<ImageButton
android:id="@+id/copy"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:padding="9dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_content_copy_black_24dp" />
<ImageButton
android:id="@+id/previous"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#0000"
android:scaleType="fitCenter"
android:src="@drawable/ic_navigate_next_black_24dp" />
</LinearLayout>
<ScrollView
android:id="@+id/questionSC"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:layout_marginBottom="30dp">
<LinearLayout
android:id="@+id/question_lo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- android:descendantFocusability="blocksDescendants"> -->
<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="web"
android:fontFamily="@font/ge_thameen_book"
android:paddingTop="10dp"
android:paddingBottom="20dp"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20dp" />
<!--android:textIsSelectable="true"
android:focusableInTouchMode="true"
android:focusable="true"/> -->
</LinearLayout>
</ScrollView>
</RelativeLayout>
这是我的Activity类中与TextView有关的部分
question = (TextView) findViewById(R.id.question);
text = PreviewText;
/* Set Text Attributes */
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Uri uri = null;
// I get the Uri from the DB
Cursor cursor = database.rawQuery("SELECT URL FROM Reference WHERE _Name ='" + Masdar + "';", null);
cursor.moveToFirst();
uri = Uri.parse(cursor.getString(0));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.parseColor("#56A846"));
ds.setUnderlineText(false);
}
};
SpannableStringBuilder str = new SpannableStringBuilder(text);
str.setSpan(new StyleSpan(Typeface.BOLD), 0, text.indexOf("\n"), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(Typeface.BOLD), text.lastIndexOf("\n") + 1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(clickableSpan, text.lastIndexOf("\n") + 1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
question.setText(str);
question.setMovementMethod(LinkMovementMethod.getInstance());
question.setHighlightColor(Color.TRANSPARENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
question.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
question.setGravity(Gravity.CENTER);
}
这是与onTouchListener相关的代码
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context ctx) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 250;
private static final int SWIPE_VELOCITY_THRESHOLD = 200;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
} /*else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
result = true;
}*/
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
我希望能够选择TextView的文本,并能够复制,共享,翻译等。