Android response button program structure

Source: Internet
Author: User

 

The buttons in the graphic interface are the most common controls. The program structure of the Android response buttons is roughly divided into the following two types:

(1) button Implementation Method for sampling anonymous internal classes:

The following Java program only derives from Activity when declaring Class, and does not implement any interfaces. Therefore, to respond to button events, you must use the anonymous internal class in the following method, such

// Exit;

Bt_exit.setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){

// Perform action on click

Finish ();

}

});

In this program, new View. OnClickListener () is an anonymous internal class, and

Public void onClick (View v ){

// Perform action on click

Finish ();

}

Is the implementation of this internal class onClick method.

The complete program structure is as follows:

Public class a15act extends Activity {

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

SetTitle ("dimming flashlight 2010-3-20 ");

// Create the TextView object in the main luminous area;

Final TextView TV _light = (TextView) findViewById (R. id. TV _light );

// Create three Button objects;

Button bt_low = (Button) findViewById (R. id. bt_low );

Button bt_high = (Button) findViewById (R. id. bt_high );

Button bt_exit = (Button) findViewById (R. id. bt_exit );

// Exit;

Bt_exit.setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){

// Perform action on click

Finish ();

}

});

// Low brightness button;

Bt_low.setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){

// Perform action on click

TV _light.setBackgroundColor (Color. GRAY );

}

});

// High Brightness button;

Bt_high.setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){

// Perform action on click

TV _light.setBackgroundColor (Color. WHITE );

}

});

}

}

 

(2) Using Activity to directly implement the OnClickListener interface:

Different from the above program structure, OnClickListener can be directly implemented when Class is declared, and then all button responses can be implemented using the same onClick method within the Class, button differentiation can be implemented using the swith -- case statement structure.

For example, the public class mmact extends Activity implements OnClickListener not only derives from the Activity but also implements the OnClickListener interface when the class is declared. In class, use a statement to implement The onClick method of OnClickListener, that is, public void onClick (View aView ). You can use switch -- case to implement many button events, for example

Switch (aView. getId ()){

Case R. id. button1:

SetTitle ("Start the App ");

EditText edt = (EditText) findViewById (R. id. edittext_name );

TextView TV = (TextView) findViewById (R. id. textview_display );

TV. setText ("Hello," + edt. getText ());

// Android: text = "YES ";

 

Break;

Case R. id. button2:

Finish ();

Break;

}

The following is a relatively complete program structure: www.2cto.com

 

Public class mmact extends Activity implements OnClickListener {

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

// Obtain the reference of the button object from the resource file

Button sButton1 = (Button) findViewById (R. id. button1 );

Button sButton2 = (Button) findViewById (R. id. button2 );

// Register the Click Event listener

SButton1.setOnClickListener (this );

SButton2.setOnClickListener (this );

}

Public void onClick (View aView ){

Switch (aView. getId ()){

Case R. id. button1:

SetTitle ("Start the App ");

EditText edt = (EditText) findViewById (R. id. edittext_name );

TextView TV = (TextView) findViewById (R. id. textview_display );

TV. setText ("Hello," + edt. getText ());

// Android: text = "YES ";

 

Break;

Case R. id. button2:

Finish ();

Break;

}

}

}

 

From jcli-blog

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.