【问题标题】:Is it possible to implement private interface or extends private class in Java?是否可以在 Java 中实现私有接口或扩展私有类?
【发布时间】:2015-09-03 07:55:45
【问题描述】:

我与 android EditText 斗争了好几天,尝试摆脱选择文本时的“粘贴”弹出窗口。 (我的扩展 EditText 是只读且可选择的)。

从回答者到问题:EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event

覆盖 package-private 方法 canPaste() 适用于 dalvik 运行时,但不适用于使用 ART 的 android 5.0 以上。

所以我找到了解决问题的其他方法,不是覆盖方法,而是尝试替换编辑器的字段变量

我看EditText 的辅助类Editor 有很多特定的逻辑,看看我是否可以在适当的时候用一个虚拟子类替换InsertionPointCursorController,它不会显示弹出窗口。 (InsertionPointCursorController是一个私有类,它实现了一个私有接口,所以要替换成一个虚拟子类,我需要扩展类或者实现接口)

public boolean performLongClick(boolean handled) {
    // Long press in empty space moves cursor and shows the Paste affordance if available.
    if (!handled && !isPositionOnText(mLastDownPositionX, mLastDownPositionY) &&
            mInsertionControllerEnabled) {
        final int offset = mTextView.getOffsetForPosition(mLastDownPositionX,
                mLastDownPositionY);
        stopSelectionActionMode();
        Selection.setSelection((Spannable) mTextView.getText(), offset);
        getInsertionController().showWithActionPopup();
        handled = true;
    }

看起来 getInsertionController().showWithActionPopup(); 显示弹出窗口 :(

现在我将尝试将删除回调替换为 anternative

【问题讨论】:

  • 你不能有一个外部的private 类。另外,我看不出您的问题与标题之间存在联系。
  • 外部接口也不能私有,只有内部接口可以。
  • privatepackage-private 是两个不同的东西。

标签: java android


【解决方案1】:

不能扩展外部私有类(默认在同一个包中,在其他包中保护可以)和接口。

扩展您需要更改的父类并覆盖您需要使用的方法,但同样您不能覆盖私有方法。 您可以重新编码它们(相当“重载”)。

【讨论】:

    猜你喜欢
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2013-05-20
    • 2017-11-06
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    相关资源
    最近更新 更多