Android Getting Started note-Interface development-Textview,button,edittext,toast

Source: Internet
Author: User

For a brief introduction to the control resources in Android today, let's start with a landing screen, with the code: prep: Create an Android project in Eclipse, my project name is Demo_login.


(1) Open Activity_main.xml in the res/layout/file directory of the project folder:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <linearlayout android:layout_width = "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <TextVi EW android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:texts Ize= "18SP" android:text= "User name:"/> <edittext android:id= "@+id/et_username" an Droid:layout_width= "Match_parent" android:layout_height= "wrap_content"/> </LinearLayout> <li Nearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:orientati on= "Horizontal" > <textview android:layout_width= "wrap_content" android:layout_height= " Wrap_content "AndroidOid:textsize= "18SP" android:text= "Password:"/> <edittext android:id= "@+id/et_password" Android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> &LT;/LINEARLAYOUT&G    T <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:ori entation= "Horizontal" > <button android:id= "@+id/btn_login" android:layout_width= "Wrap_  Content "android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:text=" Login " /> <button android:id= "@+id/btn_quit" android:layout_width= "Wrap_content" a ndroid:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "Cancel"/> </linearl Ayout></linearlayout>

(2) Then open the Mainactivity.java file under the project src/directory:

Package Com.example.demo_login;import Android.os.bundle;import Android.app.activity;import android.content.Context ; Import Android.view.menu;import android.view.view;import android.widget.button;import android.widget.EditText; Import Android.widget.toast;public class Mainactivity extends Activity {private Context mctx;private EditText musername; Private EditText mpassword;private button mlogin;private button mquit; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mCtx = Mainactivity.this;musername = (EditText) Findviewbyid (r.id.et_username); Mpassword = (EditText) Findviewbyid (R.id.et_ password); mlogin = (Button) Findviewbyid (r.id.btn_login); mquit = (Button) Findviewbyid (r.id.btn_quit); Mlogin.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {String username = Musername.gettext (). toString (); String password = Mpassword.gettext (). toString (); if (Username.equals ("admin") &AMP;&Amp Password.equals ("admin")) {Toast.maketext (Mctx, "Login Successful", Toast.length_short). Show (); Else{toast.maketext (mctx, "Login Failed", Toast.length_short). Show ();}}); Mquit.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {finish ();});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

Okay, so much for the code, and now we're going to explain:

The Activity_main.xml file is a mainactivity layout file, and Android uses XML to configure the control, which separates the interface from the logic, making development more efficient and simplifying the code. Let's see Activitt_main.xml first.

Define TextView:

        <textview            android:layout_width= "wrap_content"            android:layout_height= "Wrap_content"            android: Textsize= "18SP"            android:text= "username:"/>
Define EDITTEXT:

        <edittext            android:id= "@+id/et_username"            android:layout_width= "match_parent"            android:layout_ height= "Wrap_content"/>
To define a button:

        <button            android:id= "@+id/btn_login"            android:layout_width= "wrap_content"            android:layout_height= " Wrap_content "            android:layout_weight=" 1 "            android:text=" Landing "/>

LinearLayout: Flow layout, mainly used to control the arrangement of the control, if you do not understand, see the following chapters

TextView: Displaying fixed text

EditText: Text input box

Button: Buttons

where EditText and Button set the id attribute, it is for us to find the resource file in mainactivity and call them. Now look at how to invoke:

We see Mainactivity's OnCreate () method, which is first entered here after the program starts:

Here is a description of our layout file Activity_main.xml is used to populate the mainactivity, Setcontentview () method used to set the load of the activity of the layout file.

Setcontentview (R.layout.activity_main);
We set the two input boxes and two buttons as member variables and use the Findviewbyid (r.id.___) method to find them from the layout file:

Private EditText musername;private EditText mpassword;private button mlogin;private button mquit;
Musername = (EditText) Findviewbyid (r.id.et_username); Mpassword = (EditText) Findviewbyid (r.id.et_password); mlogin = ( button) Findviewbyid (r.id.btn_login), Mquit = (button) Findviewbyid (r.id.btn_quit);

Then we give Mlogin login button set Mouse click event, if click the Login button, will trigger the inside of the OnClick () method, inside the function is very simple, is to get from the EditText input string, and then determine whether equal to "admin", If yes, the toast on the screen pops up successfully. Say here, toast is what thing, in fact is a hint of a bubble, as long as know how to use it.

Mlogin.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {String username = Musername.gettext (). toString (); String password = Mpassword.gettext (). toString (), if (Username.equals ("admin") && password.equals ("admin")) { Toast.maketext (Mctx, "landing success", Toast.length_short). Show (); Else{toast.maketext (mctx, "Login Failed", Toast.length_short). Show ();}});
Then the click event of the Mquit button calls the Finish () method, that is, to close the activity, you do not have to explain it.


To introduce a toast:

Toast is a kind of friendly hint, the function is to give the user prompt information, but it shows the fastest, the smallest, so called cordial. Usage is common, use the method:

Toast.maketext (Mctx, "landing success", Toast.length_short). Show ();
One might ask Mctx what this parameter means, simply to say that this is the context context object, specifically for associating with activity or application-related contexts (resources), and we look at the initialization of Mctx:

Mctx = Mainactivity.this;
I prefer to create a Mctx object in each activity, which makes it easy to use, such as when creating toast or adapter. Personal use toast I actually like to encapsulate a method in activity that is specifically used to display toasts:

private void Showtoast (String words) {toast.maketext (mctx, words, Toast.length_short). Show ();
So every time I use a toast, I just need to call Showtoast (the "statement to show"). This is more concise, of course, a personal hobby.


Well, for the first time here, I think there is nothing to write, the main control is to know, will be used, of course, you can also customize the control, in the later chapters to discuss.

The following is a CHM file attached to an Android document, which you can use if you want to check the documentation. Of course, the recommendation is to Google official online to find out, was sealed, no money to turn over the wall, it will be used!




Android Getting Started note-Interface development-Textview,button,edittext,toast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.