Special usage of Android EditText

Source: Internet
Author: User

Android:digits

In Android development, it is often required to set EditText as the password display, but usually requires that the password is only a combination of letters and numbers , which can be filtered by this property.

<EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:digits="0123456789abcdefghigklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM._" />

Test effect


Digits.gif2, Setkeylistener ()

Then there is one more way to limit the EditText input character, which is to set the EditText KeyListener

Et.setkeylistener (NewNumberkeylistener () {//Limit the type of keyboard that pops up            Public   int getinputtype(){returnInputtype.type_class_number; }//Limit the characters entered            protected Char[] Getacceptedchars () {Char[] numbers =New Char[]{'. ',' 1 ',' 3 ',' 5 ',' 7 ',' 9 '};returnNumbers }        });

Test effect


SETKEYLISTENER.GIF3, the Setemptyview of the ListView

This method can set a hint view for a ListView that does not have data, and is often used to prompt when the ListView fails to load data or load data



1.sparsearray--replaces HashMap, mainly has the following several

    • Sparselongarray
    • Sparseintarray
    • Sparsebooleanarray
    • Sparsearray

Sparsearray is more memory than HashMap, it takes a compressed approach to data to represent sparse array data, thereby saving memory space, Sparsearray can only store key int type of data, and Sparsearray when storing and reading data, Using a binary lookup method

//创建一个存储String值得SparseArraynew SparseArray<String>();//存储 注意key是int类型sparseArray.put(1"zhangsan");sparseArray.put(2"123456");//通过int类型的key获取valuesparseArray.get(1);//第二个参数是默认值sparseArray.put(2"000000");//获取索引处的key与valuesparseArray.keyAt(1);sparseArray.valueAt(1);

2.ArrayMap and simplearraymap--Replacement HashMap
Where Arraymap is a subclass of map, and Simplearraymap is not.

//创建一个ArrayMap key和value都是String类型 可以是任意类型new ArrayMap<>();//存arrayMap.put("username""zhangsan");arrayMap.put("password""123456");//取arrayMap.get("username");arrayMap.get("password");


Summary description:
(1) If the type of key is an int, long, or Boolean type, use Sparsearray because it avoids the automatic boxing process;
(2) If the key type is a different type, the arraymap is used;
(3) Two data structures are suitable for a situation in which the volume is not particularly large.

Second, the API chapter

1.view.isshown () determines whether the view is displayed, does not need to be based on view.getvisibility () = = View.visible to determine whether to show or hide, but note
(1)view.getvisibility () = = View.visible only to judge the visibility of the View itself;
(2)View.isshown () returns true only if it and all its ancestors are visible.

The 2.textutils.isempty (charsequence) method, which is used to determine whether a string is null or "", should never be written again if (str! = null &&!str.equals ("" ))

3.TextWatcher interface, used to listen to the text input box content changes, this should be relatively known to more people, because there are often such requirements, the basic structure is as follows:

Textwatcher Mtextwatcher =NewTextwatcher () {@Override            Public   void beforetextchanged(charsequence charsequence, int i, int i1, int i2) {            }@Override            Public   void ontextchanged(charsequence charsequence, int i, int i1, int i2) {            }@Override            Public   void aftertextchanged(Editable Editable) {            }        };

4.textview.seterror (); This is typically used for edittext, validating user input, and then giving hints so that you don't need to use toast or dialog to prompt.


Seterror.png

5.onBackPressed (); Call this sentence, which is equivalent to a return button.

6.runOnUiThread can be run directly into the main thread, typically used in child threads, as follows:

new Thread(new Runnable() {            @Override            public void run() {                //执行耗时操作                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        //操作主线程的UI                    }                });            }        }).start();



Special usage of Android EditText

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.