【发布时间】:2019-07-07 05:03:17
【问题描述】:
我在我的活动中实现了 ontouchListener 来检测左右滑动。但是,启用此功能使我无法使我的文本可选择并相应地获得默认的 android 文本选择光标菜单。
经过多次尝试,现在我可以完美地在长按textView时调用onLongClick()方法。但是,仍然无法选择文本。此外,每当我禁用滑动检测时,文本选择都会完美运行。
public class PreviewActivity extends AppCompatActivity
{
TextView question;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preview);
question = (TextView)findViewById(R.id.question);
question.setTextSize(TextViewSize);
question.setTextIsSelectable(true);
question.setLongClickable(true);
question.setFocusableInTouchMode(true);
View prev_act = (View) findViewById(R.id.question);
prev_act.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override public void onSwipeLeft() {
if(RowIndex>0){
Qtitle = ListItems.get(RowIndex-1);
Query();
//text = Query();
//question.setText(text);
questionSC.scrollTo(0,0);
RowIndex--;
}
}
@Override public void onSwipeRight() {
if(RowIndex<ListItems.size()-1){
Qtitle = ListItems.get(RowIndex+1);
Query();
//text = Query();
//question.setText(text);
questionSC.scrollTo(0,0);
RowIndex++;
}
}
@Override public void onLongClick() {
Log.v(this.toString(), "Long click.");
question.setCursorVisible(true);
question.performLongClick();
}
});
}
OnSwipeTouchListener 类如下:
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 void onLongPress(MotionEvent e) {
onLongClick();
super.onLongPress(e);
}
@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;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
public void onLongClick() {
}
}
我希望能够从 TextView 中选择一个部分(文本)并让滑动检测器沿着它。
已编辑:添加了提到的 Activity 的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:paddingBottom="30dp"
android:background="@drawable/bg"
>
<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_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="30dp"
android:layout_marginTop="50dp">
<LinearLayout
android:id="@+id/question_lo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/ge_thameen_book"
android:paddingBottom="20dp"
android:paddingTop="10dp"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20dp"
android:autoLink="web"
/>
</LinearLayout>
</ScrollView>
【问题讨论】:
-
你可以为那个特定的 TextView 添加 xml 吗?
-
当然...我已经编辑了帖子并包含了所需的 xml 文件。每当我禁用滑动检测时,文本选择都会完美运行。
-
不幸的是,建议的解决方案不起作用......
-
使用我发布的答案尝试它,我将 textview 更改为在 textView 上实际调用
performLongClick()而不是要求焦点或任何事情,因为这将保证启动可选择的方法在 touchlistener 内部
标签: java android textview ontouchlistener