简体   繁体   English

隐藏软键盘时,从editText隐藏光标

[英]hide cursor from editText when soft keyboard is hidden

This is my first application and I am try to find a way to remove the cursor from edit box when I push the back button and the software keyboard disappeared. 这是我的第一个应用程序,当我按下后退按钮并且软件键盘消失时,我试图找到一种从编辑框中删除光标的方法。 I tried to check if the back button pressed but this works only if you push the back button two times if the keyboard is on. 我试图检查是否按下了后退按钮,但只有在键盘打开时按下后退按钮两次才有效。

Below is a sample of my code with an editText and with back button check: 下面是我的代码示例,其中包含editText和后退按钮检查:

public class MainActivity extends Activity implements OnItemSelectedListener, OnGlobalLayoutListener
{
    boolean flag;
    double vc, vs, t, r, c;
    EditText resistor_E_T;

@Override
public void onBackPressed()
{
    resistor_E_T.setCursorVisible( false );
}


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    resistor_E_T = ( EditText ) findViewById( R.id.resistor_Edit_Text );
    resistor_E_T.addTextChangedListener( new TextWatcher( )
    {
        public void afterTextChanged( Editable s ) 
        {
            resistor_E_T.setCursorVisible( true );
            if ( Exceptions.isDouble( resistor_E_T.getText( ).toString( ) ) )
            {
                r = Double.parseDouble( resistor_E_T.getText( ).toString( ) );
            }
        }

        public void beforeTextChanged( CharSequence s, int start, int count, int after ) { }

        public void onTextChanged( CharSequence s, int start, int before, int count ) { }
   });

Also, I found miscellaneous codes on the internet and in the stackoverflow that check if the keyboard is up via pixels but I couldn't make any code to work. 此外,我在互联网和stackoverflow中发现了杂项代码,检查键盘是否通过像素启动,但我无法使任何代码工作。 Does anyone have an idea how can I do this or it is impossible. 有没有人知道我该怎么做或者这是不可能的。

What happens now: http://i.stack.imgur.com/6alGK.png 现在发生了什么: http //i.stack.imgur.com/6alGK.png

When the soft keyboard disappeared cursor still blinking: http://i.stack.imgur.com/wpbkl.png 当软键盘消失时,光标仍然闪烁: http //i.stack.imgur.com/wpbkl.png

use this one:- 使用这一个: -

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

or you can use this in your xml: 或者您可以在xml中使用它:

android:windowSoftInputMode="stateHidden"

please let me know if it works or not :) 请告诉我它是否有效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM