【问题标题】:How to implement Extracted Text for a custom Android View如何为自定义 Android 视图实现提取文本
【发布时间】:2026-01-10 13:45:02
【问题描述】:

背景

Android 中的自定义编辑器视图能够通过InputConnection 从系统键盘接收文本。我已经能够成功地做出这样的看法。但是,当设备处于横向模式时,系统有时会选择显示提取的文本视图。当用户在此模式下键入时,提取的文本视图应使用与自定义视图中相同的文本进行更新。

我无法实现提取的文本视图功能。 (Here are some things I've tried.)

我也找不到任何明确的文档或完整的示例来说明如何做到这一点。 (这里有一些我读过的更好的东西:onetwothreefour)。

MCVE

我已经创建了最基本的自定义编辑器。以下 gif 显示了该功能。它可以从键盘接收文本,但不会更新提取的横向文本视图。因此,除非您关闭键盘,否则您无法看到更新后的文本。

MyCustomView.java

public class MyCustomView extends View {

    SpannableStringBuilder mText;
    Paint mPaint;

    public MyCustomView(Context context) {
        this(context, null, 0);
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setFocusableInTouchMode(true);
        mText = new SpannableStringBuilder();
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(60);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(mText, 0, mText.length(), 50, 100, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm == null) return false;
            imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);
        }
        return true;
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        outAttrs.inputType = InputType.TYPE_CLASS_TEXT;
        return new MyInputConnection(this, true);
    }
}

MyInputConnection.java

public class MyInputConnection extends BaseInputConnection {

    private MyCustomView customView;

    MyInputConnection(View targetView, boolean fullEditor) {
        super(targetView, fullEditor);
        customView = (MyCustomView) targetView;
    }

    @Override
    public Editable getEditable() {
        return customView.mText;
    }

    @Override
    public boolean commitText(CharSequence text, int newCursorPosition) {
        boolean returnValue = super.commitText(text, newCursorPosition);
        customView.invalidate();
        return returnValue;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <net.example.extractedtext.MyCustomView
        android:id="@+id/myCustomView"
        android:background="@android:color/holo_blue_bright"
        android:layout_margin="50dp"
        android:layout_width="300dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        />

</RelativeLayout>

总结

我正在寻找一个规范的答案,该答案描述并举例说明如何为自定义编辑器视图实现提取的文本更新。

如果我自己弄清楚,我会添加我自己的答案。在那之前我能做到的最好的只是disable extracted text altogether。这并不理想。

【问题讨论】:

    标签: android android-custom-view inputconnection android-extracted-text


    【解决方案1】:

    您可以只删除提取的视图。我试过了,你的自定义视图在使用键盘时是可见的。

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        outAttrs.inputType = InputType.TYPE_CLASS_TEXT;
    
        //remove extract view
        outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
    
        return new MyInputConnection(this, true);
    }
    

    【讨论】:

    • 在我的问题结束时,我还有一个指向答案的链接,该链接告诉您如何禁用提取的文本。但是,我不想禁用它。我想学习如何使用它,因为在某些情况下,键盘占用了太多空间并且看不清文字。在这些情况下最好使用提取的文本视图。
    【解决方案2】:

    您可以为此使用inputMethodManager.updateExtractedText(view, token, extractedText)

    这个方法的第一个参数很简单。您可以在那里传递 CustomView 的实例。最后一张也。只需创建 ExtractedText 并像这样设置其字段。

    ExtractedText extractedText = new ExtractedText();
    extractedText.text = "sample text";
    

    更难的是传递正确的token。要了解此参数的正确值,您可以覆盖 methodgetExtractedText(ExtractedTextRequest request, int flags) 在您的 MyInputConnection 类中(令牌存储在请求对象中)。

    @Override
    public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
            currentToken = request.token;
            return new ExtractedText();
    }
    

    我从这个方法返回空的 ExtractedText 对象来激活视图(默认文本看起来像提示)。

    你可以在这里找到我的解决方案https://github.com/ljarka/ExtractedText

    【讨论】:

    • 关键是令牌。我以为我可以忽略它并将其设置为0,但实际上需要设置。
    • 关于我如何使用你的答案来解决我更大的问题,请参阅this answer