Android studio learning notes (3): simple controls and examples


This paper gives a brief description of common controls (textview, button, EditText, RadioButton, checkbox, ImageView)

Concept Differentiation of control, component and plug-in

When it comes to controls, we have to distinguish some concepts.

Controls: components used in programming

Components: components of software

Plug in: a component in an application that has an interface reserved

Here are some examples

Android system controls:

  • Textview
  • ImageView
  • Button

Four components of Android

  • Activity
  • Service
  • Content Provider
  • Broadcast Receiver

There is no better plug-in on the web page than flash, which is used for video loading and playing.

Control specification

And layout like controls need to set at least the following propertiesandroid:layout_widthandandroid:layout_height, respectively control the width and height of the layout. Generally, the parameters to be filled in are match [parent] and wrap [content], and the specific size can also be filled in.

For practical needs,android:idIt is also necessary that the ID attribute can only accept the value of the resource type, that is, the value that must start with @ is represented by an int type value in R. For example, @ + ID / XYZ, expressed in asint xyz = value, where value is a hexadecimal number.

It is worth noting that@The difference between ID and @ + ID

  • @+ID is to add an ID name in the file. If the same ID name already exists before, the previous name will be overwritten.
  • @ID refers to the existing ID resource of the file. If it does not exist, an error will be reported.

Textview display text box

Several units commonly used in layout files

  • dp(dip): device independent pixels different devices have different display effects
  • PX: pixels: different devices display the same effect
  • SP: scaled pixels: mainly used for font display
  • pt:(point): is a standard unit of length, 1pt = 1 / 72 inch, not commonly used

Common functions that can be realized:

  • You can set pictures in four directions:drawableTop(1)drawableButtom(below)drawableLeft(left)drawableRight(right)

  • When the URL, e-mail and phone number appear in the text, we can setandroid:autoLinkProperty: when we click the corresponding part of the text, we can jump to a default app, such as a string of numbers, and then jump to the dialing interface!

  • Text effects, such as running lights, shadows, etc

  • android:ellipsize=”end”Content exceeds screen width with ellipsis at the end

  • Achieve the effect of running lamp (text cycle scrolling)

    Android: singleline = "true" // single line display
    Android: ellipseze = "marquee" // text scrolling
    Android: marqueerepeatlimit = "marquee" forever "// set to permanent loop
    Android: focuseable = "true" // get keyboard focus
    Android: focusableintouchmode = "true" // get the touch screen focus

    Here’s a brief introductionfocus, the control that gets the focus is the control that can currently perform the operation. For example, when we click on the text box, the cursor will flash. You can enter the text at the cursor, and the text box here will get the focus. If you use theandroid:maxLines=”1″It will be reported wrong.

    There is a flaw in such a design – other controls in the current interface can’t get the focus, and other operations can’t be carried out on this interface, just for the demonstration effect, and if multiple running lights are set at the same time, only the first one will respond.(I also met a pit, which was that I couldn’t show the effect of running lamp on my red rice real machine, so I need to fill in the pit.)So we need to design a running lamp without focus

The lower icon of the dynamic graph is the watermark

Android studio learning notes (3): simple controls and examples

Button button

Button generally needs to achieve the effect of a certain response after pressing, jump. Button is a subclass of textview. Textview can also set click events like button

The principle of click effect is: light color before pressing, dark color after pressing, of course, the effect will be better if the color is selected properly. One approach is toandroid:backgroundReference drawable resource file in, setting< item android:state_pressed=”xx”>When XX is true and false, the button status changes

See for the realization of button water wave effect

In the dynamic diagram, if the button state is not set to change, the button clicking effect cannot be seen, so I added the dot prompt when clicking

Android studio learning notes (3): simple controls and examples

EditText to enter a text box

Common practical properties are as follows

  • android:hintPrompt text for input box when entering
  • android:inputTypeSpecify the input type. If it is specified as the phone type, the keypad will pop up automatically, and the input only accepts the number type.
  • android:maxLines=”1″Specifies that the maximum number of lines in the input box is 1 to prevent format deformation

Demo only demonstrates the digital input. You can see that the number does not accept other types of input other than numbers

Android studio learning notes (3): simple controls and examples

RadioButton radio button

The use method is as follows

For example, if you want to work out a single choice question, the set of options is in the layout file, several nested inside, each representing an option, the content of which isandroid:textDecision.

For convenience, the default option is usually set when the user does not select it, and the property is set in this optionandroid:checked=”true”

The default style of the option is to add a dot before the text to indicate that it is selected, or to fill the solid color with a text box. We need toandroid:backgroundReference drawable resource file in, setting< item android:state_checked=”xx”>When XX is true and false, the button status changes.

Android studio learning notes (3): simple controls and examples

Checkbox check box

What distinguishes RadioButton from multi topic is that it does not need to set a set of options, but

The title can be displayed by textview, with one for each optionIndicates that the option content isandroid:textDecision.

The button style of the option can be modified in the drawable resource file by referencing the drawable resource file.

Android studio learning notes (3): simple controls and examples

ImageView image view

ImageView has two properties that you can set for pictures: SRC and background

  • Background usually refers tobackground, and Src meanscontent!!

  • When using SRC to fill in a picture, it is based on the size of the pictureDirect filling, andNo stretching

Using background to fill in the image will be based on the width given by ImageViewstretching

The image zoom type can be set in two control modes:

  • Settings in layout fileandroid:scaleTypeattribute
  • Settings in Java codeImageView.setScaleType()Method

There is no way to load network pictures in the source code of ImageView. Of course, there are many ways on the Internet. Here is one of them: use the third-party library glide to load network pictures.

Configure build.gradle according to Tiange’s video. At the beginning, an error was reported, saying the pit encountered in the modification:

  • The build.gradle to be modified is in the app directory, not another
  • The current version uses Android x, so the android.v7 package will not be found. Just change it to appcompat in the current version. The rest is the same
  • It is recommended to change the current version of compile to implementation

Android studio learning notes (3): simple controls and examples

Reference material

  1. Differences between components, controls, and plug-ins

  2. Textview, ImageView details | rookie tutorial,

  3. Android basic simple controls

  4. @The difference between ID and @ + ID

  5. [Tiange] Android development video tutorial development of the latest version of Android studio

  6. Android get focus

  7. Detailed explanation of Android’s running lamp