gpt4 book ai didi

java - TextView 无法通过 ontouchListener 选择

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:29 25 4
gpt4 key购买 nike

我在我的 Activity 中实现了 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>

最佳答案

尝试了一段时间后,这就是突出显示和选择文本以实现复制、粘贴、全选...等的方法

您需要在 prev_act.setOnTouchListener(new OnSwipeTouchListener(this) {

中添加 onTouch 代码段
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//do stuff here
Log.i("clicking", "onActionDown");
}else if(event.getAction() == MotionEvent.ACTION_UP) {

question.performLongClick();
}

return false;
}
@Override public void onLongClick() {
}

关于java - TextView 无法通过 ontouchListener 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56914408/

25 4 0
文章推荐: java - 如何正确设计具有内部条件转换的游戏状态机
文章推荐: python - Jupyter 笔记本 : 500 Internal Server Error
文章推荐: html - Firefox 显示
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com