gpt4 book ai didi

android - 以编程方式显示软键盘 - 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 13:28:51 25 4
gpt4 key购买 nike

我的屏幕上有一个切换按钮。如果我点击这个按钮,我需要一个键盘来显示在屏幕上。这是我现在的代码,但它没有按预期显示键盘:(

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
keyboard = (ToggleButton) findViewById(R.id.keyboard);
keyboard.setOnClickListener(displayKeyboard);
}

OnClickListener displayKeyboard = new OnClickListener(){
@Override
public void onClick(View v) {
if(client == null)
return;
boolean on = ((ToggleButton) v).isChecked();
if(on){ // show keyboard
System.out.println("Togglebutton is ON");
keyboard.requestFocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(keyboard, InputMethodManager.SHOW_IMPLICIT);
}
else{ // hide keyboard
System.out.println("Togglebutton is OFF");
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(keyboard.getWindowToken(), 0); }
}
};

当我单击键盘切换按钮时,我在 LogCat 中看到它进入了 if/else block ,但除此之外没有在屏幕上显示任何键盘。有人可以帮忙吗?

最佳答案

使用 showSoftInput 时,您试图聚焦您的 keyboard 按钮并开始向其发送键盘事件,但它不可聚焦。像这样使其可聚焦(在您的 onCreate 中):

keyboard.setFocusable(true);
keyboard.setFocusableInTouchMode(true);

关于android - 以编程方式显示软键盘 - 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15928597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com