这段代码是显示和隐藏Android键盘上的一个按钮点击。
public void keyClickHandler(View v) {
EditText editText = (EditText) findViewById(R.id.KeyBoard);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (keyboard) {
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
keyboard = false;
} else {
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
keyboard = true;
}
Log.d("SET", "Focus");
}
但它是在仿真器中工作的not
我发现它在手机中工作正常,但模拟器中的not
发布于 2011-12-22 04:42:37
我不知道您的其余代码如何,但您可以尝试如下所示:
public void onClick(View v)
{
EditText editText = (EditText) findViewById(R.id.KeyBoard);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
switch(v.getId())
{
case R.id.yourButtonId:
if(keyboard)
{
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
keyboard = false;
}
else
{
mgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
keyboard = true;
}
Log.d("SET", "Focus");
break;
}
}
为此,您必须使用onClickListener实现您的类,并在onCreate中将您的按钮设置为如下所示:
Button yourButton = (Button) findViewById(R.id.yourButtonId);
yourButton.setOnClickListener(this);
https://stackoverflow.com/questions/8599050
复制