How to remove focus from single editText

后端 未结 17 1426
南笙
南笙 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:44

    check this question and the selected answer: Stop EditText from gaining focus at Activity startup It's ugly but it works, and as far as I know there's no better solution.

    0 讨论(0)
  • 2020-12-13 17:45

    I will try to explain how to remove the focus (flashing cursor) from EditText view with some more details and understanding. Usually this line of code should work

    editText.clearFocus()
    

    but it could be situation when the editText still has the focus, and this is happening because clearFocus() method is trying to set the focus back to the first focusable view in the activity/fragment layout.

    So if you have only one view in the activity which is focusable, and this usually will be your EditText view, then clearFocus() will set the focus again to that view, and for you it will look that clearFocus() is not working. Remember that EditText views are focusable(true) by default so if you have only one EditText view inside your layout it will aways get the focus on the screen. In this case your solution will be to find the parent view(some layout , ex LinearLayout, Framelayout) inside your layout file and set to it this xml code

    android:focusable="true"
    android:focusableInTouchMode="true"
    

    After that when you execute editText.clearFocus() the parent view inside your layout will accept the focus and your editText will be clear of the focus.

    I hope this will help somebody to understand how clearFocus() is working.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-13 17:50

    Just include this line

    android:selectAllOnFocus="false"
    

    in the XML segment corresponding to the EditText layout.

    0 讨论(0)
  • 2020-12-13 17:52

    I know is too late, but for somebody whit the same need editText.setFocusable(false) si what you are looking for.

    0 讨论(0)
提交回复
热议问题