2011-12-06 295 views
1

我使用下面的代碼隱藏虛擬鍵盤時,我按Enter鍵,但它不隱藏。如果有人知道這個或任何代碼錯誤,請回復我。隱藏虛擬鍵盤

package onchip.learning.smalltest; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.TextView.OnEditorActionListener; 

public class SmallTest extends Activity { 
    EditText et; 

    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     et = new EditText(this); 
     et.setLines(1); 
     et.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     setContentView(et); 

     et.setOnEditorActionListener(new OnEditorActionListener() { 

      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       // TODO Auto-generated method stub 
       if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { 
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromInputMethod(et.getWindowToken(), 0); 
        return true; 
       } 
       return false; 
      } 
     }); 
    } 
} 

感謝

+0

你在模擬器中進行測試呢? – Cata

+0

不在設備上 –

回答

2

嘗試使用hideSoftInputFromWindow(et.getWindowToken(), 0)代替hideSoftInputFromInputMethod()

+0

thankyou Cata其工作 –

+0

歡迎您! :) – Cata

0

使用此

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);