简体   繁体   中英

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:

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. Does anyone have an idea how can I do this or it is impossible.

What happens now: http://i.stack.imgur.com/6alGK.png

When the soft keyboard disappeared cursor still blinking: 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:

android:windowSoftInputMode="stateHidden"

please let me know if it works or not :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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