The EditText control of Android is a very commonly used control. It is used most often for login and registration pages. It provides users with an intuitive and convenient input box. This article briefly summarizes some of the settings commonly used in the EditText control, and provides two methods for implementation of each setting, one is to implement in the layout file, the other is dynamic code settings in the program.
1. How to add a box
In the Hololight topic of Android, The EditText control has only one blue horizontal line at the bottom by default. How can I add a box to your EditText?
Layout ]:
Set the android: background property to give it a white image with a rectangle, or customize a rectangular drawable file.
For example:
android:background="@drawable/shape_bg"
Code ]:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);mEditText.setBackgroundResource(R.drawable.shape_bg);
2. How to set the font size, color, and bold
Layout ]:
In the layout, the attributes are android: textSize, android: textColor, and android: textStyle.
For example:
android:padding="15sp"android:textSize="15sp"android:textStyle="bold"
Code ]:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);mEditText.setTextSize(15);mEditText.setTextColor(Color.BLACK);mEditText.setTypeface(Typeface.DEFAULT_BOLD);
3. How to set a password for display
Layout ]:
Set the android: password attribute to true.
For example:
android:password="true"
Code ]:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);mEditText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
4. How to disable text breaks
Layout ]:
Set the android: singleLine attribute to true.
For example:
android:singleLine="true"
Code ]:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);mEditText.setSingleLine();
5. How to set prompt information when no input is available
Layout ]:
Set the value of the android: hint attribute
For example:
android:hint="input your name"
Code ]:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);mEditText.setHint("Input your name");
6. How to leave a few characters at the beginning of the line in the input box
Layout ]:
Set the android: paddingLeft attribute.
For example:
android:paddingLeft="15sp"
Code ]:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);mEditText.setPadding(15,0,0,0);
7. How to limit the input length
Layout ]:
Set the value of the android: maxLength attribute.
For example:
android:maxLength="10"
Code ]:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);InputFilter[] filters = new InputFilter[1];filters[0] = new InputFilter.LengthFilter(10);mEditText.setFilters(filters);
8. How to restrict the input type from numbers, phone numbers, dates, and times?
Layout ]:
You can set the android: inputType attribute to specify textPassword, phone, number, date, time, and other types.
For example:
android:inputType="text"
Code ]:
EditText mEditText = (EditText) findViewById (R. id. MyEditText); mEditText. setInputType (InputType. TYPE_CLASS_TEXT); // There are many types of InputType available
9. How to restrictOnly the specified characters can be entered.
Layout ]:
Set the android: digits attribute.
For example:
android:digits = "abcdef"
Code ]:
There are two ways to achieve this:
Method 1:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);String digits = "abcdef";mEditText.setKeyListener(DigitsKeyListener.getInstance(digits));
Method 2:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);InputFilter[] filters = new InputFilter[1];filters[0] = new MyInputFilter("abcdef");mEditText.setFilters(filters);public class MyInputFilter extends LoginFilter.UsernameFilterGeneric { private String mAllowedDigits; public PopInputFilter( String digits ) { mAllowedDigits = digits; } @Override public boolean isAllowed(char c) { if (mAllowedDigits.indexOf(c) != -1) { return true; } return false; }}
10. Make the font size of the entered password consistent with that of the plain text.
After you set the android: password = "true" attribute, you will find that its font size is different from that of EditText without the password attribute. Therefore, if you want them to behave in the same way, you can use the following code:
EditText mEditText = (EditText)findViewById(R.id.MyEditText);mEditText.setTypeface(Typeface.DEFAULT);mEditText.setTransformationMethod(new PasswordTransformationMethod());
The attribute settings of EditText are summarized here. If you find any problems later, add them. If you have any questions, please leave a message or send a letter to lujun. hust @ gmail.
This article from the "three shadows" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1333414