How to get started with Android Development (2): how to get started with android Development

Source: Internet
Author: User

How to get started with Android Development (2): how to get started with android Development
Preface:

The previous issue achieved a simple QQ login effect and continued to expand the previous period.

Current knowledge points:

Toast pop-up window, three methods to implement button click event listening

Body:
 

The Toast pop-up window is actually very simple. You can press the tab key to quickly generate a toast in Android Studio.

 
Toast. makeText (CurrentActivity. this, "content", Toast. LENGTH_SHORT). show ();
The first parameter is context (where to display Toast), and the second parameter is a String, that is, the content displayed by Toast, the third is the display time of Toast, and short is short.
 
 

In this way, the Toast is displayed. I changed the code in the previous pop-up dialog box to the following code. this refers to the current activity, which can be omitted.

 

 

I have not explained the listener clearly. In the example above, we have implemented a listener interface for the current activity and then rewritten its onClick method, find the button instance through findviewbyid and call setOnClickListener to bind the listener to it. Then, when you click the button, it will enter the onClick method, which consists of v. getid obtains the view id clicked by the current user, and then enters a switch branch statement. id. if the button is the same, execute this branch, that is, a Toast pops up, not just a button, other textview, linearlayout, and other controls or the root layout can also set the listener.

 

I have summarized five methods for implementing the time listener, which may not be correct. However, you only need to know the method, so you don't have to worry about the details.

How to Implement the event listener:

  • Internal class form
  • External form
  • Activity itself acts as an event listener class
  • Anonymous internal class form
  • Bind directly to the tag

 

Internal class form

Define the event listener class inside the current class

  

Public class Test extends Activity {@ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button button = (Button) findViewById (R. id. about); MyButtonlistener listener = new MyButtonlistener (); button. setOnClickListener (listener);} class MyButtonlistener implements View. onClickListener {@ Override public void onClick (View v) {// handle related events }}}

 

 

 

 

 

 

External class form

Test class

public class Test extends Activity{    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button button = (Button)findViewById(R.id.about);        MyButtonlistener listener = new MyButtonlistener();        button.setOnClickListener(listener);    }}

MyButtonlistener class

Public class MyButtonlistener implements View. OnClickListener {@ Override public void onClick (View v) {// event processing }}

 

 

Activity itself acts as an event listener class

Use the Activity itself as the event listener class

  

Public class Test extends Activity implements View. onClickListener {@ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button button = (Button) findViewById (R. id. about); button. setOnClickListener (this);} @ Override public void onClick (View v) {// event processing }}

 

Anonymous internal class form
Public class Test extends Activity {@ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button button = (Button) findViewById (R. id. about); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// event processing }});}}

 

 

 

Bind directly to the tag

Add the onClick attribute to the xml layout file to set related methods.

 

Define a method. Remember that the parameter is View.

 

For more information, see this article: listener-based event processing for Android development.

 

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.