简体   繁体   English

从静态方法中调用抽象类内部的非静态方法

[英]Call a non-static method inside an abstract class from a static method

I'm trying to detect when the input method picker called by InputMethodManager.showInputMethodPicker() is closed or changed. 我试图检测何时关闭或更改了InputMethodManager.showInputMethodPicker()调用的输入法选择器。 I found a possible solution proposed by Sherif elKhatib in another question: How can I tell if the input method picker is open or closed? 我在另一个问题中找到了Sherif elKhatib提出的一种可能的解决方案: 如何确定输入法选择器是打开还是关闭? . His answer suggests that the OP should use an abstract non-static class. 他的回答表明,OP应该使用抽象的非静态类。 However, I don't know how to call a method from the abstract class in a static method. 但是,我不知道如何在静态方法中从抽象类中调用方法。 I thought I'd open a separate question for it here because the original question is already old and inactive. 我以为我会在这里为此开一个单独的问题,因为原始问题已经很旧并且不活跃。

Here is the solution introduced by Sherif: 这是Sherif引入的解决方案:

public abstract class InputMethodActivity extends FragmentActivity {
    protected abstract void onInputMethodPicked();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mState = NONE;
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(mState == PICKING) {
            mState = CHOSEN;
        }
        else if(mState == CHOSEN) {
            onInputMethodPicked();
        }
    }

    private static final int NONE = 0;
    private static final int PICKING = 1;
    private static final int CHOSEN = 2;
    private int mState;
    protected final void pickInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showInputMethodPicker();
        mState = PICKING;
    }
}

The method I'd like to call is pickInput() in order to get a response from onInputMethodPicked() . 我想调用的方法是pickInput() ,以便从onInputMethodPicked()获得响应。

However, simply called pickInput(); 但是,简称为pickInput(); from a static method doesn't work, and won't even find it. 静态方法无法正常工作,甚至找不到。

Furthermore, InputMethodActivity.pickInput(); 此外, InputMethodActivity.pickInput(); gives the error " Non-static method 'pickInput()' cannot be referenced from a static context ". 给出错误“ 无法从静态上下文中引用非静态方法'pickInput()' ”。

Next, I tried to instantiate it, but I found out that abstracts cannot be instantiated: InputMethodActivity instant = new InputMethodActivity(); 接下来,我尝试实例化它,但是我发现摘要无法实例化: InputMethodActivity instant = new InputMethodActivity(); gives the error " 'InputMethodActivity' is abstract; cannot be instantiated ". 给出错误“ 'InputMethodActivity'是抽象的;无法实例化 ”。

After further reading, I tried to create an anomynous class: InputMethodActivity anonym = new InputMethodActivity() {}; 进一步阅读后,我尝试创建一个统一的类: InputMethodActivity anonym = new InputMethodActivity() {}; anonym InputMethodActivity anonym = new InputMethodActivity() {}; , but this gives the error "Class ' Anonymous class derived from InputMethodActivity' must either be declared abstract or implement abstract method 'onInputMethodPicked()' in 'InputMethodActivity' ". ,但这会导致错误“ 必须从抽象声明类或在'InputMethodActivity'中实现抽象方法'onInputMethodPicked()'来声明类 ”。 I thought both of them were already declared abstract, so I'm nearing the end of my ropes here. 我以为它们已经被声明为抽象了,所以我在这里快要结束了。

Problem: 问题:

Basically, I would like to know if it's possible to run pickInput() in a static method, such as a public void onClick_TextView(View v){} , and how it can be achieved. 基本上,我想知道是否可以在静态方法(例如public void onClick_TextView(View v){} pickInput()中运行pickInput() ,以及如何实现该方法。

abstract class you can only inherit it and write different implemnetaion for abstract method like onInputMethodPicked method in different derived classes and by inheritance you can use any method from InputMethodActivity (parent class) in MainActivity (child class) like pickInput method 抽象类只能继承它,并为抽象方法(如onInputMethodPicked方法)在不同的派生类中编写不同的实现,通过继承,您可以使用MainActivity (子类)中InputMethodActivity (父类)的任何方法,如pickInput方法

   public class MainActivity extends InputMethodActivity implements     View.OnTouchListener {
     @Override
     protected  void onInputMethodPicked(){
     // your implemetion after pick 
    }
    @Override
    public void onClick(View view) {
     pickInput();
   }

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM