How to use common space in Android
1.TextView
TextView is primarily used to display a piece of textual information on the interface, as shown in the following code:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/text_view"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center"android:textsize= "24SP"Android:textcolor= "#00ff00"Android:text= "This is TextView" /></LinearLayout>
Among them, in the TextView tag:
Android:id defines a unique identifier for the current control;
ANDROID:LAYOUT_WIDTH Specifies the width of the control, where match_parent indicates the size of the current control and the size of the parent layout;
Android:layout_height Specifies the height of the control, wrap_content means that the size of the current control can contain exactly what is inside;
ANDROID:TEXT specifies what is displayed in the TextView.
Android:gravity to specify the alignment of the text, you can use the ' | ' To specify multiple values at the same time;
ANDROID:TEXTSIZE Specifies the size of the text in the TextView;
ANDROID:TEXTCOLOR Specifies the color of the text in the TextView.
In addition to the above, there are many other attributes in TextView.
2.Button
A button is an important control that the program uses to interact with the user. For example, the following code defines a button:
< Button Android:id = "@+id/button" android:layout_width= "Match_parent" android:layout_height= "Wrap_ Content " android:text=" button/>
After adding the button, you can register a listener for the button's click event in Mainactivity, as shown in the following code:
Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {}}); } }
Once the listener is registered, the onclick () method in the listener is executed, simply by adding the pending logic to the method.
The above code to the button to join the Listener method is to use anonymous classes, you can also use to implement the interface to register, the code is as follows:
Public classMainactivityextendsActivityImplementsOnclicklistener {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener ( This); } Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.button: //Add logic here Break;default: Break; } }}
The above code can also be used to monitor button click events.
3.EditText
EditText is an important control that the program uses to interact with the user, allowing the user to enter and edit content in the control, and to process the content in the program.
The code for placing a EditText control in the interface is as follows:
< EditText Android:id = "@+id/edit_text" android:layout_width= "Match_parent" android:layout_height= "Wrap_ Content " android:hint=" Type something here " android:maxlines = "2" />
The use of Android controls is basically similar, by defining an ID for the control, specifying the width and height of the control, and then adding other properties of the control to it. Such as:
The Android:hint property is to display some suggestive text in the input box, and then after the user enters the content, the hint text disappears.
The Android:maxlines property is the maximum number of rows for the specified edittext, so that when the input exceeds two lines, the text scrolls up.
You can combine EditText and button to perform some functions, such as clicking on a button to get the content entered in EditText, as shown in the following code:
Public classMainactivityextendsActivity {PrivateEditText EditText; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); EditText=(EditText) Findviewbyid (R.id.edit_text); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {String Inputtext=Edittext.gettext (). toString (); Toast.maketext (mainactivity. This, Inputtext, Toast.length_short). Show (); } }); }}
The above code first obtains the EditText instance through the Findviewbyid () method, then calls EditText's GetText () method in the button's Click event
Gets the input, calls the ToString () method into a string, and then displays the content using the Toast method.
4.ImageView
ImageView is a control that is used to display pictures on the interface. The following code can show the Ic_launcher.png picture under the Drawable folder:
< ImageView Android:id = "@+id/image_view" android:layout_width= "Wrap_content" android:layout_height= "Wrap_ Content " android:src=" @drawable/ic_launcher "/>
You can also change the picture in ImageView dynamically by code in the program, as shown in the following code:
Public classMainactivityextendsActivity {PrivateImageView ImageView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ImageView=(ImageView) Findviewbyid (R.id.image_view); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//in the button listener, call ImageView's Setimageresource () method to modify the displayed picture. Imageview.setimageresource (R.drawable.icon); } }); }}
The above code, in the button's click event, calls ImageView's Setimageresource () method to display the picture as icon.
5.ProgressBar
ProgressBar is used to display a progress bar on the interface, indicating that our program is loading some data, and the ProgressBar usage is shown in the following code:
< ProgressBar Android:id = "@+id/progress_bar" android:layout_width= "Match_parent" android:layout_height= "Wrap_ Content " style="? android:attr/progressbarstylehorizontal " android: Max= "/>"
The above code can display a progress bar on the interface, where style specifies that the progress bar is a horizontal style, and the default is circular if no style is added.
How do you make the progress bar disappear when the data is loaded? This will use the visual properties of the Android control, and all Android controls have this property.
Can be specified by android:visibility, three values are selectable:
Visible indicates the visibility of the control, which is the default value;
Invisible indicates that the control is not visible, but it still occupies the original position and size;
Gone indicates that the control is not visible and no longer occupies any screen.
The visibility of the space can also be set in code, using the Setvisibility () method, which can be passed into view.visible, viewinvisible, and View.gone.
The following code is used to set the visibility of the progress bar:
Public classMainactivityextendsActivity {PrivateProgressBar ProgressBar; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ProgressBar=(ProgressBar) Findviewbyid (R.id.progress_bar); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//to set the visibility of a progress bar if(progressbar.getvisibility () = =View.gone) {progressbar.setvisibility (view.visible); }Else{progressbar.setvisibility (view.gone); } intProgress =progressbar.getprogress (); Progress= Progress + 10; Progressbar.setprogress (progress); } }); }}
The above code through the Getvisibility () method to determine whether the ProgressBar is visible, if visible will progressbar hidden away;
The ProgressBar is displayed if it is not visible.
6.AlertDialog
Alertdialog can pop up a dialog box at the current interface, which is pinned to all interface elements to
Shielding the interactivity of other controls, so Alertdialog is generally used to prompt for some very important content or warning messages.
The Alertdialog usage is shown in the following code:
/*** Alertdialog can pop up a dialog box at the current interface, which is pinned to all * interface elements and can mask the interactivity of other controls. */ //first, an instance of Alertdialog is created by Alertdialog.builder. Alertdialog.builder dialog =NewAlertdialog.builder (mainactivity. This); //Set TitleDialog.settitle ("This is dialog"); //Set ContentDialog.setmessage ("Something Important"); //set Whether you can cancelDialog.setcancelable (false); //Call the Setpositivebutton () method to set the Click event for the dialog box to determine the buttonDialog.setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { } }); //Call the Setnegativebutton () method to set the Cancel button's Click event for the dialog boxDialog.setnegativebutton ("Cancle",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { } }); //call the Show () method to display the dialog boxdialog.show ();
7.ProgressDialog
Like ProgressDialog and Alertdialog, you can pop up a dialog box on the interface to mask the interactivity of other controls;
The difference is that ProgressDialog will display a progress bar in the dialog box, which generally means it is time consuming and allows the user to wait patiently.
The usage of ProgressDialog is shown in the following code:
/*** ProgressDialog and Alertdialog are similar, and both display a dialog box in the interface. * The difference is that ProgressDialog will display a progress bar in the dialog box, which generally means: * The current operation is more time-consuming, allowing the user to wait patiently. */ //first, a ProgressDialog object is constructed. ProgressDialog ProgressDialog =NewProgressDialog (mainactivity. This); //setting the caption of a dialog boxProgressdialog.settitle ("This is ProgressDialog"); //setting up content messagesProgressdialog.setmessage ("Loading ..."); //setting can be canceled if False is passed in setcancelable (), indicating that ProgressDialog cannot be canceled by the back key.//when the data is loaded, you must call ProgressDialog's dismiss () method to close the dialog box. Progressdialog.setcancelable (true); //call the Show () method to display the dialog boxProgressdialog.show ();
Note: The above code has passed true in setcancelable (), indicating that ProgressDialog can be canceled by the back key;
If False is passed in, it means that ProgressDialog cannot be canceled by the back and must be called when the data load is complete.
ProgressDialog the Dismiss () method to close the dialog box, otherwise ProgressDialog will persist.
Android Learning notes (eight)