TextView не может быть выбран - PullRequest
0 голосов
/ 04 июля 2019

По какой-то причине я не могу выделить текст моего TextView. в качестве обзора:

1 - Я реализовал onTouchListener, где обнаруживаю пролистывание влево и вправо.

2 - У меня есть TextView внутри scrollView

3 - Я выполняю некоторую обработку текста в TextView, в котором у меня есть определяемый кликом интервал и другие свойства диапазона.

Я пробовал следующее:

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>

А вот части, касающиеся 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 и копировать, делиться, переводить и т. Д.

...