How can I tell if the input method picker is open or closed?

前端 未结 3 1897
孤城傲影
孤城傲影 2020-12-18 22:12

My app opens the input method picker (the menu where you choose a keyboard) with InputMethodManager.showInputMethodPicker(). My app doesn\'t actually create the

3条回答
  •  时光取名叫无心
    2020-12-18 22:51

    There is no such mechanism for check InputMethodPicker is open or not.

    But you can check it via another way like as using hasWindowFocus() method for check has focus of your root layout.

    Below is sample code:

    Main.xml

    
    
    
        
    
        

    DemoappActivity.class

    public class DemoappActivity extends Activity {
        /** Called when the activity is first created. */
    
    
        Button btn1 , btn2;
        InputMethodManager imeManager;
    
        LinearLayout mLayoutRoot;
        TimerTask timertask;
        Timer timer;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mLayoutRoot = (LinearLayout)findViewById(R.id.mainlayout);
            imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
            btn1 = (Button)findViewById(R.id.btnPicker);
            btn1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    showInputMethodPicker();
                }
            });
            btn2 = (Button)findViewById(R.id.btnCheck);
            btn2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    timer.cancel();
                }
            });
            checkMyWindowHasFocus();
        }
        @Override
        protected void onDestroy() {
    
            timer.cancel();
            super.onDestroy();
        }
    
        public void checkMyWindowHasFocus()
        {
            timertask = new TimerTask() {
    
                @Override
                public void run() {
                    System.out.println("has window focus..."+mLayoutRoot.hasWindowFocus());
                    runOnUiThread(new Runnable() {
    
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Has focus "+mLayoutRoot.hasWindowFocus(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            };
            timer = new Timer();
            timer.schedule(timertask, 500, 5000);
    
        }
    
        private void showInputMethodPicker() {
    
            if (imeManager != null) {
                imeManager.showInputMethodPicker();
    
            } else {
                Toast.makeText(this, "Error",Toast.LENGTH_LONG).show();
            }
        }
    }
    

提交回复
热议问题