2012-02-20 129 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