我只是刚刚发现了
by
关键词
class
override fun suggestSelection(...}
,它似乎没有回来
fallback.suggestSelection(text, selectionStartIndex, selectionEndIndex, defaultLocales);
. 如您所见,它所做的只是在中调用相同的方法
fallback
用同样的论点。这是唯一的办法还是有什么办法
退路
inner class MyTextClassfier(private val fallback:TextClassifier) : TextClassifier by fallback
{
override fun suggestSelection(
text: CharSequence,
selectionStartIndex: Int,
selectionEndIndex: Int,
defaultLocales: LocaleList?
): TextSelection
{
return fallback.suggestSelection(text, selectionStartIndex, selectionEndIndex, defaultLocales);
}
按以下要求添加完整代码。它需要Android Studio生成的默认Android项目结构。
主要活动。kt
package com.stackoverflow.test
import android.content.Context
import android.os.Bundle
import android.os.LocaleList
import android.util.Log
import android.view.textclassifier.TextClassification
import android.view.textclassifier.TextClassificationManager
import android.view.textclassifier.TextClassifier
import android.view.textclassifier.TextSelection
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textClassificationManager = getSystemService(Context.TEXT_CLASSIFICATION_SERVICE) as TextClassificationManager;
val defaultOne = textClassificationManager.textClassifier;
val txt = findViewById<TextView>(R.id.textview1);
txt.setTextClassifier(MyTextClassfier(defaultOne));
}
inner class MyTextClassfier(private val fallback:TextClassifier) : TextClassifier by fallback
{
override fun suggestSelection(
text: CharSequence,
selectionStartIndex: Int,
selectionEndIndex: Int,
defaultLocales: LocaleList?
): TextSelection
{
return fallback.suggestSelection(text, selectionStartIndex, selectionEndIndex, defaultLocales);
//return TextSelection.Builder(0,0).build();
}
override fun classifyText(
text: CharSequence,
startIndex: Int,
endIndex: Int,
defaultLocales: LocaleList?
): TextClassification
{
//Selection ended. User has lifted his finger.
Log.d("stack", "classifyText is called.");
return TextClassification.Builder().build();
}
}
}
主要活动。xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textview1"
android:text="@string/large_text"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textIsSelectable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>