Since I learned android, I felt that controls could not overlap. For example, I could not put another TextView in an EditText. I felt that the controls had a location and could not overlap. However, when I read the Android text message source code this week, I found that the controls can be put together. This shows that Google has taken this into consideration when designing the Android language, therefore, the designer cleverly implemented this function. For example, if you can enclose an image in a TextView and place a TextView in EditText, I wrote an EditViewDemo example and placed a TextView in the lower right corner of EditText, display the number of words that can be entered. A total of 160 words can be entered. If you have any questions or want the source code, you can leave a message.
Effect of the program at the beginning: when you enter a letter TextView, the remaining number is displayed:
Results When multiple letters are entered:
The code is provided, and everything has to be seen in the code to tell the truth:
1. mainActivity. Code in the java class:
Package com.cn. daming;
Www.2cto.com
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. OS. Bundle;
Import android. text. Editable;
Import android. text. TextWatcher;
Import android. view. View;
Import android. widget. EditText;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private AlertDialog dialog;
Private View show_dialog;
Private EditText edit_text;
Private TextView text_view;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Dialog = new AlertDialog. Builder (MainActivity. this). create ();
Show_dialog = View. inflate (MainActivity. this, R. layout. edittext_dialog, null );
Edit_text = (EditText) show_dialog.findViewById (R. id. edit_text );
Text_view = (TextView) show_dialog.findViewById (R. id. text_view );
Dialog. setView (show_dialog );
Dialog. show ();
Edit_text.addTextChangedListener (new EditTextWatcher ());
}
// EditTextWatcher is listener the editText changed
Public class EditTextWatcher implements TextWatcher {
Public void afterTextChanged (Editable arg0 ){
}
Public void beforeTextChanged (CharSequence cs, int arg1, int arg2,
Int arg3 ){
Edit_text.setVisibility (View. VISIBLE );
If (edit_text.getText (). length () <160 ){
Text_view.setText ("" + (160-(edit_text.getText (). length ()));
}
Else {
Edit_text.setFocusable (true );
}
}
Public void onTextChanged (CharSequence cs, int arg1, int arg2,
Int arg3 ){
}
}
}
II. The code in edittext_dialog.xml is the key to the code design. Some Properties of RelativeLayout are cleverly used to achieve control overlap:
<Span style = "font-size: 16px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1.0"
>
<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
>
<EditText
Android: id = "@ + id/edit_text"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentRight = "true"
Android: maxLines = "5"
Android: maxLength = "2000"
/>
<TextView
Android: id = "@ + id/text_view"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_alignBottom = "@ id/edit_text"
Android: textColor = "# ffff0000"
/>
</RelativeLayout>
</LinearLayout>
</Span>
3. Code in AndroidManifest. xml:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com.cn. daming"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "8"/>
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>