1. TextView: Display text
Common property settings:
Android:textisselectable= "true" to set the text content optional (triggering the system's copy-and-paste function)
android:drawableleft= "@mipmap/ic_launcher" to set the text content icon
Others include Drawableright, Drawabletop, Drawablebottom, Drawablestart, Drawableend
android:text= "Hello world!" Setting the text content displayed
Android:textcolor= "@color/coloraccent" to set the text color
Android:textsize= "24SP" to set text font size
Android:textstyle= "Bold" to set the font style (bold bold, normal default, italic italic)
Android:clickable= "True" if you can click
Android:onclick= "" can be clicked, you can set the Click event
Android:ellipsize= "Marquee" to set the text content beyond the display range
End, marquee, middle, none, start
android:lines= "1" Setting the number of rows displayed
android:maxlines= "1" Maximum number of rows
android:minlines= "1" minimum number of rows
Android:gravity setting the location of text content
android:background= "@color/colorprimary" setting the background color
2. EditText: Input box (TextView subclass)
Used to receive input text content
Common Properties:
Android:hint= "" Displays a message when the input box has no content
Other attributes are similar to TextView
Two event monitoring:
1, input change of event monitoring:
Addtextchangedlistener (New Textwatcher () {
Beforetextchanged before text changes
ontextchanged text is changing
Aftertextchanged after the text changes
});
2. Edit the Completed Action Monitor (carriage return)
Setoneditoractionlistener (New Oneditoractionlistener () {
Oneditoraction () to perform edits after the completion of the action (such as submit content)
});
3. Button: Buttons (set click events)
You typically use the button component's Click event to submit data, or to trigger an action
1, the implementation of the button event monitoring three ways:
<1> by implementing Listener interfaces: View.onclicklistener, usually implemented in the activity interface (or other interface classes)
Implementation of the event processing method: OnClick (View v), by judging the parameter component ID, to determine which button the user is operating
Switch (V.getid ()) To implement different button actions
Add event handling for each button (Setonclicklistener (this))
Recommended (highest effect) when handling multiple button events
<2> using anonymous inner classes to set event listeners directly to buttons
Setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
//......
}
});
The benefit of using this approach is that the code set is concise and small when used, because using anonymous inner classes generates separate classes, which can affect certain
The performance.
<3> using properties in a layout file to customize the way you click events
Using the android:onclick= "method name" in the layout file
Custom method in activity class: public void Method Name (View v) {}, Benefits concise, but with certain limitations
The above three methods: preferably the first kind
2, configure the style of the button:
<1> Configure transparent button styles
Use the Style property on the button component in the Configuration layout file:
Style= "? Android:attr/borderlessbuttonstyle"
or set
Background= "@null"
<2> Configure the style of the custom button:
Using Selector (selector)
Create a configuration file under the Res/drawbale directory, such as Button_bg.xml
In the file:
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<!--state_pressed means press events, drawable can be color or background picture--
<item android:state_pressed= "true" android:drawable= "@color/button_pressed"/>
<!--Configure default styles--
<item android:drawable= "@color/button_default"/>
</selector>
Then use the Background property in the layout settings of the button to refer to the resource configuration file:
background= "@drawable/BUTTON_BG"
4. ImageButton: Graphic button (customizable button shape, effect)
Similar to button buttons, represents a Picture button
1, specifies that the picture is displayed on the button by using the src attribute
src= "@mipmap/ic_launcher"
You can also set the picture selector by defining a selector. For example, in Drawable/imagebutton_bg.xml
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_pressed= "true"
android:drawable= "@mipmap/de_btn_main_add_hover"/>
<item android:drawable= "@mipmap/de_btn_main_add"/>
</selector>
2. Define the shape of the button (can be applied on all buttons)
Define Drawable/button_shape.xml
<!--
Rectangle Rectangle
Corners Angle
padding inner margin
Line of the stroke boundary
-
<shape xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:shape= "Rectangle" >
<solid android:color= "@color/coloraccent"/>
<corners
Android:bottomleftradius= "10DP"
Android:bottomrightradius= "10DP"
Android:topleftradius= "10DP"
android:toprightradius= "10DP"/>
<padding
Android:bottom= "5DP"
android:left= "5DP"
android:right= "5DP"
android:top= "5DP"/>
<stroke android:color= "#000000" android:width= "5DP"/>
</shape>
5. ImageView: Display Graphics ()
Represents a component that displays a picture
Specify the picture resource to display through the SRC attribute
When displaying a picture, be aware that it is displayed in the appropriate size, if a large number of large pictures (in memory) appear in an interface, then
There is a memory overflow situation, so it is important to display the appropriate image at the appropriate size
Display the picture in the right way: Zoom in on the picture
<1> define the size of the component so that the picture fits the component in a way that automatically scales the display by aspect ratio
MaxHeight Maximum height of picture display
MaxWidth the maximum width of the picture display
Adjustviewbounds the width and height ratio of the picture
The method will be scaled by the original scale
<2> using ScaleType to zoom in on a picture
Matrix is drawn using matrices to dynamically zoom in and out of images to show
Fitxy The picture is not scaled up/down to the view size display
Fitstart enlarge/Shrink the image to the width of the view, displayed in the upper part of the view
Fitcenter enlarge/Shrink the picture to the width of the view, centered on the display
Fitend Zoom in/out to the width of the view, displayed in the lower part of the view
Center displays the original size of the image, and when the picture is longer/wider than the length/width of the view, the center portion of the image is displayed
Centercrop proportionally enlarges the size of the image so that the image is long (wide) equal to or greater than the length (width) of the view
Centerinside Displays the contents of the picture in full, by scaling it down or the original size to make the picture long/wide equal to or
Less than the length/width of the view
Android:tint renders the picture as a specified color.
Commonly used as: center, Centercrop
Other properties:
Android:clickable= "True" if you can click to set the Listener event when the property is True
Note: Do not display large pictures in widgets (the width height of the picture component is less than the width of the picture itself)
6. CheckBox: check box
Used to provide multiple choices
Android:checked= "true" setting check box is selected by default
There are usually two ways to get the contents of a CheckBox component:
<1> single click event with one button to check if checkbox is selected
Checkbox.ischecked () determines whether the selected
Get the text content of the check box again by Checkbox.gettext (). ToString ()
Checkbox.setchecked (Boolean) to set whether to select
<2> by adding a Compoundbutton.oncheckedchangelistener event to the checkbox component
To listen for check boxes to select or deselect events.
7, RadioButton and Radiogroup: Group puts radiobutton in radiogroup (mutual exclusion, can be used for bottom navigation)
To implement multiple radio button mutexes, you need to put multiple radio buttons within a radiogroup group
One of the RadioButton can be set to the default check: Checked= "true"
Horizontal or vertical arrangement by orientation property in Radiogroup
To listen for radio button events, you can implement the Radiogroup.oncheckedchangelistener interface by implementing the
Then get the currently selected RadioButton object via Radiogroup.findviewbyid (Checkedid)
Then get the text content of the selected radio button by Radiobutton.gettext ()
Or get RadioButton text content with a click of a button event
Use RadioButton to implement the bottom navigation bar:
Principle: Using RadioButton's mutex function to realize the function of the bottom navigation menu bar
1. Use Radiogroup to combine multiple radiobutton at the bottom of the interface and arrange horizontally
2. Set the relevant properties of RadioButton to adjust the style:
Android:button= "@null" to remove the small circle of RadioButton
android:layout_weight= "1" split the position of multiple radiobutton
android:drawabletop= "@drawable/..." Specify the icon above
android:gravity= "Center" center display
Android:textcolor= "" Defines the font color
Where: Drawabletop,textcolor can set selector to change the selected and unchecked effects
8. ToggleButton and switch: Switch button
We can use the off button to enable user actions to turn a feature on or off
texton= "Open" button to turn on status display text
textoff= "Close" button to turn off status display text
In code, the state of the button is judged by the ischecked method.
Events that monitor the switch button:
Setoncheckedchangelistener () Setting the event of a switch change
Or we can use a separate button for unified event control.
9. Ratingbar components: Scoring bar (usually customized for better results)
Can be used to score users, products, user experience, etc.
<1> as an indicator that only provides viewing
Set to indicator by android:isindicator= "true"
<2> as a user-accessible rating
Android:isindicator= "false" default value user can click
Common Properties:
android:numstars= "5" sets the total number of stars
Android:stepsize= "1" Set step (increment value can be set each time, can be decimal)
Android:rating= "3" to set the current number of stars
Event Monitoring:
When the user changes the number of stars in the scoring bar, the listener interface can be used: Onratingbarchangelistener implementation
Ratingbar.setrating (4f);
Ratingbar.setonratingbarchangelistener (New Ratingbar.onratingbarchangelistener () {
@Override
public void onratingchanged (Ratingbar ratingbar, float rating, Boolean fromuser) {
Ratingbar Scoring Bar components
Rating the current changed score value
Whether the Fromuser is a user action
if (fromuser) {//is a user action
///
System.out.println ("Number of stars currently set, rating=" +rating);
}
}
});
Note: Use Wrap_content when using the scoring bar, otherwise the number of stars will not be controlled, misleading the user
Use a custom scoring bar: (used in the project)
1. Define a configuration file under drawable:
Layer-list Layer List
<layer-list xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item
Android:id= "@android: Id/background"
android:drawable= "@mipmap/star_small_bg"/>
<item
Android:id= "@android: Id/progress"
android:drawable= "@mipmap/star_small_hl"/>
</layer-list>
2. Define a style in the Style.xml file:
<style name= "Mysmallratingbar" parent= "Android:Widget.RatingBar" >
<item name= "android:progressdrawable" > @drawable/ratingbar_small_bg</item>
<item name= "Android:maxheight" >16dp</item>
<item name= "Android:minheight" >16dp</item>
</style>
3. Define the Ratingbar component in the layout file
Referencing a custom style with the Style property
style= "@style/mysmallratingbar"
10. Spinner components: drop-down list
By providing a set of option data that allows the user to select
Implement a spinner component:
1. Define the spinner component in the layout file
<spinner
Android:width= "Wrap_content"
android:height= "Wrap_content"
Android:id= "@+id/spinner_city" >
2. Provide data to spinner drop-down list
<1> by string array resource file definition
The Res/values/strings.xml file defines:
<string-array name= "City" >
<item> Beijing </item>
<item> Shanghai City </item>
<item> Shenzhen </item>
<item> Wuhan </item>
<string-array>
Res/values/arrays.xml file definition: Same as Strings.xml file definition
<2> by providing in the code
By acquiring data from other places (such as networks, databases, etc.)
3, using an adapter to
Data is populated into the spinner component
Get the data from the resource file and create an adapter
arrayadapter<charsequence> adapter = Arrayadapter.createfromresource ();
Createfromresource () Parameter:
<1>context: Context
<2> Data Resource ID
layouts for displaying data in the <3> list
By directly creating the Arrayadapter:
Get Resource Object
string[] Citys = Getresources (). Getstringarray (r.array.city);
arrayadapter<charsequence> adapter = new Arrayadapter<charsequence> (
This,r.layout.spinner_item,citys);
Customize the drop-down list layout:
Using a custom layout file
Arrayadapter<charsequence> Adapter =arrayadapter.createfromresource (This,
R.array.city,r.layout.spinner_item);
Spinner_item layout files typically use the android default ID: @android: ID/TEXT1
Gets the value that is currently selected in the drop-down list
String city = Spinner_city.getselecteditem (). toString ();
11, Autocompletetextview: Automatic completion (when input data can give user prompt, login interface commonly used)
Suggest options for users
Common Properties:
android:completionthreshold= "1" Enter the number of characters, display prompt options
Android:completionhint= "Please select a city" prompt message
android:dropdownselector= "#def9de" Select the color of the drop-down list
Provides a list of recommendations that are provided through the adapter
arrayadapter<charsequence> Arrayadapter = Arrayadapter.createfromresource (
This,r.array.city,android. R.layout.simple_list_item_1);
Autocompletetextview.setadapter (Arrayadapter);
12, ProgressBar: Progress bar component (can be used to load data, give users a better experience)
Two types of styles:
1, the Dial style (round, has been rotating)
2. Horizontal style (provides visual of the progress value, usually used to load the action tip for determining the time)
The default is the dial style, if you want a horizontal style, by setting the Style property
<progressbar
Style= "? Android:attr/progressbarstylehorizontal"
When the progress bar is a deterministic value, we can set the following properties:
Android:max= the maximum value of the "100" progress bar
android:progress= "50" Current progress value
android:secondaryprogress= "80" Current second progress value
If it is a horizontal style, it can also be set to progress indeterminate by the Indeterminate property
Android:indeterminate= "True" true indicates that the progress bar is indeterminate and false indicates
If true, the style of the progress bar will scroll horizontally for repeat
ProgressDialog dialog Box progress bar
To display a progress bar style as a dialog box:
Can be a dial style or a horizontal style
ProgressDialog PD = new ProgressDialog (this);
The default is the dial style
Set to Horizontal style:
Pd.setprogressstyle (progressdialog.style_horizontal);//set to horizontal style
Pd.setmax (100);//Set Maximum progress value
Pd.setmessage ("Trying to load in ...");
Pd.settitle ("Load data");
Pd.setcancelable (TRUE);//do not allow cancellation
Listen for events that are closed by the dialog box
Pd.setondismisslistener (New Dialoginterface.ondismisslistener () {
@Override
public void Ondismiss (Dialoginterface dialog) {
Dialog.cancel ();
}
});
Pd.show (); Show
Simulation Download:
Private voidShowDialog () {FinalProgressDialog PD =NewProgressDialog ( This); Pd.setprogressstyle (progressdialog.style_horizontal); Pd.settitle (Download); Pd.setmessage ("Trying to download ..."); Pd.setmax (100); Pd.setprogress (0); Pd.setcancelable (false); Pd.show (); //using a thread to simulate a download operation NewThread (NewRunnable () {@Override Public voidrun () { while(true){ //determines whether the progress value is the maximum value, if the download is complete, cancels the dialog box, ends the thread if(Pd.getprogress () >=Pd.getmax ()) {Pd.cancel (); Break; } Pd.incrementprogressby (10);//Growth Progress Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }}}). Start (); }
Small series of words: This article simple summary, if there are errors, hope that Daniel can give correct, extremely grateful. At the same time hope that this article can help more people, work together and progress together. This article is the first blog post for Android technology, you are welcome to read, you will have more technology to share. I wish you all the best in the Process Ape Road # Hard work # struggle @everyone
Android Interface (UI) interface Controls Brief Introduction 01