我使用下面的代碼隱藏虛擬鍵盤時,我按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;
}
});
}
}
感謝
你在模擬器中進行測試呢? – Cata
不在設備上 –