EditText: how to enable/disable input?

2019-01-18 23:26发布

I have a 7x6 grid of EditText views. I want all of them disabled when the application starts, ie they should behave like normal TextViews and not to be editable. Then the user taps one cell in the grid, it changes its background and performs something visual. If the user clicks on the cell one more time it should allow editing. I'm struggling with OnClick() and OnFocusChange() listeners, but I can't accomplish such a basic interaction.

Playing with setEnabled() and setFocusable() doesn't help. I wonder why even a simple task like this has been made so difficult on Android

7条回答
SAY GOODBYE
2楼-- · 2019-01-19 00:02

Try using this setFocusableOnTouch() instead of setFocusable() method.

查看更多
劫难
3楼-- · 2019-01-19 00:04

Firstly write these line in yours xml in edittext

android:enabled="false"

And than use the code in java as shown below

Boolean check = true;
            EDITEXT.setEnabled(check);
            check=!check;
查看更多
相关推荐>>
4楼-- · 2019-01-19 00:09

I finally found a solution. It's a matter of calling

  • setFocusableInTouchMode(boolean)
  • setFocusable(boolean)

when the EditText is first created, so it can intercept the clicks. Then one can set those flags back again to make the EditText editable, request the focus, and manually show/hide the soft keyboard with InputMethodManager methods

查看更多
闹够了就滚
5楼-- · 2019-01-19 00:18

Setting input type to null is not enough, since it only suppress soft keyboard and if device has hardware keyboard, there will be input. So in order to suppress any editing you should do following:

editText.setInputType(InputType.TYPE_NULL);
editText.setFilters(new InputFilter[]
    {
        new InputFilter()
        {
            public CharSequence filter(CharSequence src, int start,
            int end, Spanned dst, int dstart, int dend)
            {
                return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
            }
        }
    });

That will guarantee that EditText content won't be changed

查看更多
家丑人穷心不美
6楼-- · 2019-01-19 00:22

You can do the follwoing:
If you want to make edittext not editable then use following method

edittext.setInputtype(Null);

If you want to make edittext editable then use the same method and set the proper inputype visit the following link for more info

查看更多
Deceive 欺骗
7楼-- · 2019-01-19 00:28

Since you are using gridview to achieve your concern you can do the following.

  1. setOnItemClicklistener on gridview
  2. Extend Edittext to make your own edittext view

The extedned class will contain boolean property named editable using this property in onItemclicklisterner of gridviewyou can call setEditable or setFocusabel or both for a editetext.

If you share your code i can elaborate more on this issue.

查看更多
登录 后发表回答