简体   繁体   中英

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. 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? . His answer suggests that the OP should use an abstract non-static class. 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:

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() .

However, simply called pickInput(); from a static method doesn't work, and won't even find it.

Furthermore, InputMethodActivity.pickInput(); gives the error " Non-static method 'pickInput()' cannot be referenced from a static context ".

Next, I tried to instantiate it, but I found out that abstracts cannot be instantiated: InputMethodActivity instant = new InputMethodActivity(); gives the error " 'InputMethodActivity' is abstract; cannot be instantiated ".

After further reading, I tried to create an anomynous class: 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' ". 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.

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

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

   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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