Can we limit the number of character in edittext o

2019-01-26 11:12发布

Is there any api to set the limit no. of characters in SearchView ?

5条回答
甜甜的少女心
2楼-- · 2019-01-26 11:27
EditText et = new EditText(this);
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
et.setFilters(FilterArray);
查看更多
Anthone
3楼-- · 2019-01-26 11:38
EditText et = (EditText)searchView.findViewById(searchView.getContext().getResources()
            .getIdentifier("android:id/search_src_text", null, null));
    et.setFilters(new InputFilter[] { new InputFilter.LengthFilter({max_text_length}) });
查看更多
家丑人穷心不美
4楼-- · 2019-01-26 11:44

Use this code it will do search only if length is less or equal to 5.You can make it change accordingly like returning true from onQueryTextChange() when text>5.

    final SearchView searchView = (SearchView) mSearchMenuItem.getActionView();     
          searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getSearchComponentName()));
          searchView.setIconifiedByDefault(true);
          searchView.setSubmitButtonEnabled(true);
          searchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onQueryTextChange(String arg0) {
                if(arg0.length()>5){
                    System.out.println("Text character is more than 5");
                    searchView.setQuery(arg0.substring(0,5), false);
                }
                return false;
            }
        });
查看更多
贼婆χ
5楼-- · 2019-01-26 11:45

I am using following code snippet:

TextView et = (TextView) searchView.findViewById(R.id.search_src_text);
        et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-26 11:50

In xml Layout try this..

 android:maxLength="20" //20 is number of characters.

It may help you..

查看更多
登录 后发表回答