Android entry notes-interface development-TextView, Button, EditText, Toast

Source: Internet
Author: User

Android entry notes-interface development-TextView, Button, EditText, Toast
Today, I will briefly introduce the control resources in android. We will start from a login interface and post the code: preparation: Create an android project in eclipse, and my project name is demo_login.


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

     
          
           
       
      
          
           
       
      
          
           
       
  
 

(2) Open the MainActivity. java file under the src/directory of the project:

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") & password. equals ("admin") {Toast. makeText (mCtx, "Login successful", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (mCtx, "Logon 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 present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

Well, there are so many codes. Now let's start to explain:

The activity_main.xml file is the layout file of MainActivity. android uses xml to configure controls. The Presentation Interface and logic are separated to make development more efficient and simplify code. Let's first look at activitt_main.xml

Define TextView:

        
 
Define EditText:

        
 
Define Button:

        

LinearLayout: stream layout, which is mainly used to control the control arrangement. If you do not understand it, see the following section.

TextView: Display fixed text

EditText: text input box

Button: Button

Here, EditText and Button set the id attribute so that we can find the resource files in MainActivity and call them. Now let's take a look at how to call it:

We can see the onCreate () method of MainActivity. After the program is started, it will first enter here:

The layout file activity_main.xml is used to fill the MainActivity. The setContentView () method is used to set the layout file for loading the Activity.

setContentView(R.layout.activity_main);
We set both the two input boxes and 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 set a mouse click event for the mLogin login button. If you click the login button, The onClick () method in the event will be triggered. The functions in the event are simple, it is to get the input string from EditText and determine whether it is equal to "admin". If so, the Toast login is successful on the screen. Here, Toast is actually a bubble of prompt, as long as you 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, "Login successful", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (mCtx, "Logon Failed", Toast. LENGTH_SHORT ). show ();}}});
Then the Click Event of the mQuit button calls the finish () method, that is, to close the Activity, so you don't have to explain it.


Toast:

Toast is a kind of kind reminder. It serves as a kind of reminder to the user, but it is the fastest and least displayed, so it is called kind. Usage is very common. Usage:

Toast. makeText (mCtx, "Login successful", Toast. LENGTH_SHORT). show ();
Someone may ask what mCtx means. Simply put, this is a Context object that is used to associate the Context (Resource) related to the Activity or Application. Let's look at mCtx initialization:

mCtx = MainActivity.this;
I prefer to create an mCtx object in each Activity. This makes it easy to use. For example, it is easy to create Toast or Adapter. I personally use Toast. I actually like to encapsulate a method in Activity to display Toast:

private void showToast(String words){Toast.makeText(mCtx, words, Toast.LENGTH_SHORT).show();}
In this way, when Toast is used, I only need to call showToast ("Statement to be displayed "). This is more concise, of course, a personal hobby.


Okay. Let's talk about it for the first time. I don't think there is anything to write about. If you want to use the control, you can use it. Of course, you can also customize the control, we will discuss it later.

The chm file of the Android document is attached below. If you want to check the document, you can use this file. Of course, we recommend that you go to the google official website to check the website. If you are blocked and have no money to go over the wall, you will use it!




Related Article

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.