How to remove focus from single editText

后端 未结 17 1501
南笙
南笙 2020-12-13 17:24

In my application I have a single EditText together with some TextViews, button and a spinner. My EditText receives focus since it is the only focu

17条回答
  •  佛祖请我去吃肉
    2020-12-13 17:45

    i had a similar problem with the editText, which gained focus since the activity was started. this problem i fixed easily like this:

    you add this piece of code into the layout that contains the editText in xml:

        android:id="@+id/linearlayout" 
        android:focusableInTouchMode="true"
    

    dont forget the android:id, without it i've got an error.

    the other problem i had with the editText is that once it gain the first focus, the focus never disappeared. this is a piece of my code in java, it has an editText and a button that captures the text in the editText:

        editText=(EditText) findViewById(R.id.et1);
        tvhome= (TextView)findViewById(R.id.tv_home);
        etBtn= (Button) findViewById(R.id.btn_homeadd);
        etBtn.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v)
            {
                tvhome.setText( editText.getText().toString() );
    
                //** this code is for hiding the keyboard after pressing the button
                View view = Settings.this.getCurrentFocus();
                if (view != null) 
                {  
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
                //**
    
                editText.getText().clear();//clears the text
                editText.setFocusable(false);//disables the focus of the editText 
                Log.i("onCreate().Button.onClickListener()", "et.isfocused= "+editText.isFocused());
            }
        });
        editText.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                if(v.getId() == R.id.et1)
                {
                    v.setFocusableInTouchMode(true);// when the editText is clicked it will gain focus again
    
                    //** this code is for enabling the keyboard at the first click on the editText
                    if(v.isFocused())//the code is optional, because at the second click the keyboard shows by itself
                    {
                        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
                    }
                    //**
    
                    Log.i("onCreate().EditText.onClickListener()", "et.isfocused= "+v.isFocused());
                }
                else
                    Log.i("onCreate().EditText.onClickListener()", "the listener did'nt consume the event");
            }
        });
    

    hope it will help to some of you!

提交回复
热议问题