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(经傻蛋测试可以正常运行)

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

    editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); //点击EditText时,不会弹出一个全屏输入窗口
    editText.setInputType(InputType.TYPE_NULL); //点击EditText时,屏蔽软键盘
    作者:快乐出发0220 ;Android群:151319601 ; Linux群:96394158 ;转载请注明出处 http://klcf0220.cnblogs.com/ !!!
  • 相关阅读:
    haproxy报错解决
    haproxy安装
    gitlab配置webhook报错解决
    git_push报错
    DNS配置
    centos7 选定默认启动内核,及删除无用内核
    ansible安装、配置ssh、hosts、测试连接
    公司手机打卡app时间和百度时间差30秒解决
    所有编辑语言的共性内容元素
    php使用正则函数使用详解
  • 原文地址:https://www.cnblogs.com/klcf0220/p/3080420.html
Copyright © 2011-2022 走看看