Java如何从toggleSoftInput获取EditText的输入

Java如何从toggleSoftInput获取EditText的输入,java,android,android-edittext,Java,Android,Android Edittext,我通过在画布上绘制EditText来创建它,因此我在单击时显示键盘和使EditText接收输入时遇到问题。经过一些搜索,我发现我可以使用InputMethodManager使键盘显示为EditText提供输入 不幸的是,EditText不会从软键盘上按下的任何东西接收任何字符输入 我需要使用imm.toggleSoftInput(InputMethodManager.SHOW\u强制,0)50&&event.getX()50&&event.getY()

我通过在画布上绘制EditText来创建它,因此我在单击时显示键盘和使EditText接收输入时遇到问题。经过一些搜索,我发现我可以使用InputMethodManager使键盘显示为EditText提供输入

不幸的是,EditText不会从软键盘上按下的任何东西接收任何字符输入

我需要使用
imm.toggleSoftInput(InputMethodManager.SHOW\u强制,0)imm.showsoftwinput(editText,InputMethodManager.SHOW_IMPLICIT)时按下EditText时,软键盘根本没有出现

代码:

if((int)event.getX()>50&&event.getX()50&&event.getY()<200){
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
InputMethodManager imm=(InputMethodManager)getContext().getSystemService(Context.INPUT\u方法\u服务);
imm.toggleSoftInput(InputMethodManager.SHOW_强制,0);
}
如何解决这个问题?欢迎任何可行的解决办法。谢谢

这应该可以

EditText edit = (EditText) view.findViewById(R.id.passwordtextvalue);
        edit.setOnKeyListener(this);

        InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        edit.requestFocus();
我的应用程序中有这个,当我使用软键盘时,它可以很好地编辑文本

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

我不能使用通常的xml EditText类型,它们是同一类型的嗯,我指的是创建时的常规类型,能够自动使键盘显示,并在单击时接收输入。然而,在我的问题中,我需要对它进行编码,以使相同的东西工作。问题已编辑。edit.setOnKeyListener(this)中的“this”不适用于我,即使“this”已引用扩展SurfaceView的类,该类也是一个视图。你知道吗?你可以通过调用getApplicationContext(),getContext(),getBaseContext()来获取上下文。这其中的任何一个都应该有效。我认为它有效,因为你把它放在活动类中了?我在一个名为GamePanel的类中运行所有东西,该类扩展了SurfaceView。因此,我需要另一种方式来调用“this”(getApplicationContext和getContext对我来说都不起作用),感谢您提供解决方案,因为这里的事情是,您只需要在特定活动中使用上下文。您应该以某种方式获取应用程序上下文以使其正常工作通常我可以使用getContext()传递上下文。e、 g.Toast.makeText(getContext(),…);但我不知道为什么它对这个不起作用。有没有其他方法将edittext连接到InputMethodManager?嗯,正如我在问题中所说的,我也尝试过这种方法,但根本没有键盘出现。如果有一种方法将editText作为参数,但仍然使用toggle,我认为它将解决这个问题
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);