2 csh 34 csh_34 于 2013.06.13 11:29 提问

如何实现隐藏虚拟键盘的功能?

我使用下面的代码来实现当我点击回车键后隐藏虚拟键盘,但是没有实现隐藏。
请问错误出在哪啊?

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;
            }
        });
    }
}

1个回答

cj6585256
cj6585256   2013.06.13 15:07

InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(int showFlags, int hideFlags);

用这个方法就应该可以了

Csdn user default icon
上传中...
上传图片
插入图片