隐藏虚拟键盘

时间:2011-12-06 15:29:00

标签: android

当我点击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;
            }
        });
    }
}

感谢

2 个答案:

答案 0 :(得分:2)

尝试使用hideSoftInputFromWindow(et.getWindowToken(), 0)代替hideSoftInputFromInputMethod()

答案 1 :(得分:0)

使用此

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
相关问题