Path to Android Development 7-UI component 2
Today, we continue to learn about the UI components, mainly including TextView and EditText.
TextView component introduction:
Subclass:
Button, CheckdTextView, Cheronometer, DigitalClock, EditText
Indirect subclass:
AutoCompleteTextView, CheckBox, CommpoundButton, MultiAutoCompleteTextView
In its xml file, there are many attributes mentioned today, for example:
Android: autoLink: when the text is set as a URL link/email/phone number, the text is displayed as a clickable link, optional value: (none/web/email/phone/map, this attribute is not described today/all)
Android: autoText: If this parameter is set, the spelling of the input value is automatically corrected, which takes effect when the input method is displayed and entered.
Android: drawableButton: enter a drawable image below text. If a color is specified, the background color of text is set to this color, and overwrites the latter when used with the background.
Android: gravity: Set the text location. You can set it to left, center, and right for left, center, and right.
Android: inputType: Set the text type
..................................
The following describes how to use the TextView component to implement the Program. This is three different hellowords. The difference is that the labels in TextView are used.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
// Original helloword
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textColor = "#00FF00"
Android: padding = "30dp"
Android: textSize = "30dp"
Android: layout_margin = "50dp"
Android: text = "@ string/hello"/>
// Add color -- textcolor and size -- textSize
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: gravity = "center"
/>
// Set the text center format
Android: gravity = "center"
</LinearLayout>
The interface formed after running is like this.
,
This is the different effects of text formed by tags.
Add background image for text
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: padding = "10dp"
Android: layout_margin = "10dp"
Android: textColor = "# cccccc"
Android: text = "@ string/hello"
Android: background = "@ drawable/ic_launcher"
/>
Using the above code, you can add a background image for your text. Although this is a background image, it may make the image distorted and stretch the image at will. This requires us to have a good grasp of photoshop. If we want to take the picture p well, it will not be distorted.
Add link for text
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: autoLink = "web"
Android: text = "@ string/webUrl"
/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: autoLink = "email"
Android: text = "@ string/email"
/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: autoLink = "phone"
Android: text = "@ string/phoneNumber"
/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: autoLink = "all"
Android: text = "@ string/autoAll"
/>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/textview"
/>
The interface is like this.
All links are added.
Custom TextView with border
Public class BorderTextView extends TextView {
Public BorderTextView (Context context, AttributeSet attr ){
Super (context, attr );
}
Public void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Paint paint = new Paint ();
Paint. setColor (android. graphics. Color. GREEN );
Canvas. drawLine (0, 0, this. getWidth ()-1, 0, paint );
Canvas. drawLine (0, 0, 0, this. getHeight ()-1, paint );
Canvas. drawLine (this. getWidth ()-1, 0, this. getWidth ()-1,
This. getHeight ()-1, paint );
Canvas. drawLine (0, this. getHeight ()-1, this. getWidth ()-1,
This. getHeight ()-1, paint );
}
}
<Cn. class3g. activity. BorderTextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: padding = "30dp"
Android: text = "xxxxxxxxxxxxx"
/>
Application of the EditText component
EditText is a subclass of TextView and inherits most of the xml attributes of TextView. Therefore, its usage is roughly the same as that of TextView.
Some specific strings
<Edittext
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Password = "true"
Android: digits = "01234"/>
// This is the setting text that can be input by those characters. The above command is that the character in 01234 can be input into text editing.
<Edittext
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: digits = "ABCD"/>
// This is the setting text that can be input by those characters. The above command is that the characters in ABCD can be input into text editing.
<Edittext
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: inputtype = "Number"/>
<Edittext
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Password = "true"
/>
// This is the password setting in the text box. As long as the character is entered, it will be displayed in the form of the origin in the interface
<Edittext
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: inputType = "textEmailAddress"
/>
// Set the email address. If you enter a character, @ will be prompted in the editing area.
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: inputType = "number"
/>
// It must be a numeric character
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: numeric = "decimal | signed"/>
// Display the text box
Register the OnKeyListener event for the EditText object and implement the onKey () method.
<EditText
Android: id = "@ + id/text1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "text1"/>
<Button
Android: id = "@ + id/button1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
Android: text = "Button"/>
Public boolean onKey (View view, int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_ENTER ){
Btn. setText (et. getText ());
Et. setVisibility (View. GONE );
Btn. setVisibility (View. VISIBLE );
}
Return true;
}
This will happen after you press the Enter key.