zoukankan      html  css  js  c++  java
  • 调用Android中的软键盘

    我们在Android提供的EditText中单击的时候,会自动的弹 出软键盘,其实对于软键盘的控制我们可以通过InputMethodManager这个类来实现。我们需要控制软键盘的方式就是两种一个是像 EditText那样当发生onClick事件的时候出现软键盘,还有就是当打开某个程序的时候自动的弹出软键盘。

    public class InputMethodManagerTest extends Activity implements OnClickListener{  
        private Button button;  
       
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            // TODO Auto-generated method stub  
            super.onCreate(savedInstanceState);  
            LinearLayout layout=new LinearLayout(this);  
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);  
            button=new Button(this);  
            button.setId(123);  
            button.setText("Hello GaoMatrix");  
            button.setOnClickListener(this);  
            layout.addView(button, layoutParams);  
            setContentView(layout);  
       
            /**  
             * 用一个定时器控制当打开这个Activity的时候就出现软键盘  
             */  
            Timer timer=new Timer();  
            timer.schedule(new TimerTask() {  
                @Override  
                public void run() {  
                    InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
                    inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
                }  
            }, 2000);  
        }  
        /**  
         * 当单击事件的时候触发显示软键盘  
         */  
        @Override  
        public void onClick(View v) {  
            InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
            inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
        }  

    这 个InputMethodManager类里面的toggleSoftInput方法的API中的解释是: This method toggles the input method window display. If the input window is already displayed, it gets hidden. If not the input window will be displayed. 这个方法在界面上切换输入法的功能,如果输入法出于现实状态,就将他隐藏,如果处于隐藏状态,就显示输入法。 而对于第二中方式进入Activity就自动显示软键盘,在一个定时器中,也就是在一个线程中执行,只不过是延迟2秒执行,原因是在onCreate函数 中Android程序未将屏幕绘制完成。

    当然我们还可以单独控制软键盘的显示和隐藏

     

    //显示  
            InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);  
            imm.showSoftInput(view, 0);    
       
            //隐藏  
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 

     

    上述代码中的view是比如Button点击时事件的那个View,或者是Activity整个布局之类的Layout(经傻蛋测试可以正常运行)

    其他关于键盘的控制还有两个经常用到:

    1. editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); //点击EditText时,不会弹出一个全屏输入窗口

    2.editText.setInputType(InputType.TYPE_NULL); //点击EditText时,屏蔽软键盘

     

  • 相关阅读:
    安装mysql
    工坊第十天
    工坊第九天
    友链qaq
    About me
    好耶
    [Ynoi2011]初始化
    [Ynoi2013]大学
    [Ynoi2015]盼君勿忘
    [Ynoi2019模拟赛]Yuno loves sqrt technology III
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4176347.html
Copyright © 2011-2022 走看看