2012-02-20 128 views
0

我正在用虚拟键盘在android上挣扎。问题是我无法把它拿起来/可靠地隐藏起来。Android虚拟键盘未显示

下面是一个拒绝在我测试的三款设备(中兴通讯Blade,Galaxy Nexus和宏碁A500)上打开键盘的代码。

我可以隐藏键盘,我可以切换它,但我无法显示它。我可以使用切换来显示/隐藏它,但它不够可靠(因为似乎无法查看键盘是否显示)。

的完整代码,看看你能不能让它工作:

package com.test.keyboardtest; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
    public class KeyboardTestActivity extends Activity { 
    LinearLayout myLayout = null; 
    TextView myView = null; 
    Button toggleButton = null; 
    Button showButton = null; 
    Button hideButton = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     myLayout = new LinearLayout(this); 
     myView = new TextView(this); 
     showButton = new Button(this); 
     hideButton = new Button(this); 
     toggleButton = new Button(this); 

     myLayout.setOrientation(LinearLayout.VERTICAL); 
     myLayout.addView(myView); 
     myLayout.addView(showButton); 
     myLayout.addView(hideButton); 
     myLayout.addView(toggleButton); 


     showButton.setText("Show Keyboard"); 
     showButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       mgr.showSoftInput(myView, 0); 
      } 
     }); 

     hideButton.setText("Hide Keyboard"); 
     hideButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       mgr.hideSoftInputFromWindow(myView.getWindowToken(), 0); 
      } 
     }); 

     toggleButton.setText("Toggle Keyboard"); 
     toggleButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);     
       mgr.toggleSoftInput(0,0); 
      } 
     }); 
     this.setContentView(myLayout);   
    } 
} 
+0

实际检测键盘是否可见并不难:http://stackoverflow.com/questions/2150078/android-is-software-keyboard-shown – 2012-02-20 11:17:47

+0

那么,你建议的代码不会总是工作。这取决于很多因素。 例如,在窗口上设置FLAG_LAYOUT_NO_LIMITS-flag会阻止我进入GlobalLayout事件。 我可以在需要时启用/禁用该标志,但它只能从UI线程调用。 – Habba 2012-02-21 08:49:14

+0

那么* * * * * * * * * * * * * * *范围* * * *提供了*信息*,它会工作。您可能会在键盘上上下跳动,同时发出狂野的猴子尖叫声,这可能也会导致问题。 – 2012-02-21 09:54:18

回答

0

试试这个

来说明键盘:

Activity.this.getWindow()setSoftInputMode(WindowManager.LayoutParams .SOFT_INPUT_STATE_ALWAYS_VISIBLE);

要隐藏键盘:

InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(),0);

+0

没有办法。当我恢复活动时,它只会启动键盘。 – Habba 2012-02-20 11:41:12