Determine the number of input EditText and androidedittext of custom controls in Android
A requirement for a recent project is to determine the number of characters that can be entered, that is, to listen to the text changes in EditText. The function is implemented, but the combination method is used, it is better to write a custom control every time. In view this article, it is recommended to first refer to I reproduced an article: http://blog.csdn.net/android_jiangjun/article/details/39580253
The topic of this article is as follows:
First, let's take a look:
The custom controls in this article use the composite controls to customize controls. The layout of custom controls is as follows:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "# fff"> <EditText android: id = "@ + id/edit" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "top | left" android: minLines = "5"/> <TextView android: id = "@ + id/text" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: hint = "You can also enter 25 characters" android: textSize = "10dip" android: layout_alignBottom = "@ id/edit" android: layout_alignRight = "@ id/edit" android: layout_marginRight = "3dip" android: layout_marginBottom = "3dip"/> </RelativeLayout>
Then the corresponding custom HintEdit class code is as follows:
Package com. gc. testcustomedittext; import android. annotation. suppressLint; import android. content. context; import android. content. res. typedArray; import android. text. editable; import android. text. inputFilter; import android. text. textWatcher; import android. util. attributeSet; import android. view. layoutInflater; import android. widget. editText; import android. widget. linearLayout; import android. widget. relativeLayout; import android. widget. textView;/***** @ author Android General **/public class HintEditText extends RelativeLayout implements TextWatcher {private EditText mEditText; private TextView mTextView; private int maxLength = 0; private RelativeLayout comment; @ SuppressLint ("NewApi") public HintEditText (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} public HintEditText (Context context) {super (context);} public HintEditText (Context context, AttributeSet attrs) {super (context, attrs); TypedArray mTypedArray = context. obtainStyledAttributes (attrs, R. styleable. hintEditText); maxLength = mTypedArray. getInt (R. styleable. hintEditText_maxLength, 0); mRelativeLayout = (RelativeLayout) LayoutInflater. from (context ). inflate (R. layout. custom_edittext, this, true); mEditText = (EditText) mRelativeLayout. findViewById (R. id. edit); mTextView = (TextView) mRelativeLayout. findViewById (R. id. text); mTextView. setHint ("You can also enter" + maxLength + "word"); // you can specify the maximum number of mEditText characters that can be entered. setFilters (new InputFilter [] {new InputFilter. lengthFilter (maxLength)}); mEditText. addTextChangedListener (this);} public void initUI (Context context) {RelativeLayout mRelativeLayout = (RelativeLayout) LayoutInflater. from (context ). inflate (R. layout. custom_edittext, this, true); mEditText = (EditText) mRelativeLayout. findViewById (R. id. edit); mTextView = (TextView) mRelativeLayout. findViewById (R. id. text) ;}@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {// TODO Auto-generated method stub} @ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {// TODO Auto-generated method stubmTextView. setHint ("can also be input" + (maxLength-s.toString (). length () + "word") ;}@ Overridepublic void afterTextChanged (Editable s) {// TODO Auto-generated method stub }}
The code for the attrs. xml file is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <declare-styleable name = "HintEditText"> <attr name = "maxLength" format = "integer"/> <! -- MaxLength is the property of the custom control. After the custom property is defined, you can directly set the property in the xml file. -->
</declare-styleable></resources>
The file code for the main layout is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:HintEditText="http://schemas.android.com/apk/res/com.gc.testcustomedittext" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.gc.testcustomedittext.HintEditText android:id="@+id/hint_edt" android:layout_width="match_parent" android:layout_height="wrap_content" HintEditText:maxLength="30" /> <com.gc.testcustomedittext.HintEditText android:layout_width="match_parent" android:layout_height="wrap_content" HintEditText:maxLength="50" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="sahjdsahjd " /></LinearLayout>
The MainActivity code is as follows:
Package com. gc. testcustomedittext; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem;/***** @ author Android General **/public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main );}}
This definition control can be used directly. You only need to copy the layout file and the HintEditText class of the custom control to your project directory and use it like the main layout and MainActivity.
Reprinted please indicate the source: http://blog.csdn.net/android_jiangjun/article/details/39580715
In the android EditText control, how to judge and limit that only numbers can be entered
Use the android: numeric = "decimal" attribute for Decimals
Then, convert the data type after gettext.
There are actually many solutions.
You can add a TextChangedListener listener for character judgment.
Android: digits attribute input rules
For example, android: digits = "0123456789" indicates that only numbers are allowed.
Android: digits = "0123456789." indicates that the number and decimal point can be entered.
In Android Listview, how does one obtain the input value of EditText and obtain it after the input is complete? I always enter a character to determine how to do it.
You asked this question ...... The system cannot determine when your input is complete ...... Unless you perform Intelligent Analysis Based on the sentence, you can only enter one word as a sign of input completion.