Android button monitoring methods

Source: Internet
Author: User

------------------- First, all buttons are listened.
Package gongzibai. co. cc;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. Toast;

Public class RE1Activity extends Activity implements

OnClickListener {

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button mButton = (Button) findViewById

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

(R. id. button2 );
TextView mTextView = (TextView) findViewById

(R. id. textView1 );
MButton. setOnClickListener (this );
MButton2.setOnClickListener (this );

}

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Toast. makeText (RE1Activity. this, "1000 ",

2000). show ();

}

}


---------------- Internal listener
Public class AnonymousListener extends Activity
{
EditText show;
Button bn;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Show = (EditText) findViewById (R. id. show );
Bn = (Button) findViewById (R. id. bn );
// Directly use Activity as the event listener
Bn. setOnClickListener (new OnClickListener ()
{
// Implement the event processing method www.2cto.com
@ Override
Public void onClick (View v)
{
// TODO Auto-generated method

Stub
Show. setText ("bn button clicked! ");
}
});
}
}

----------- Rely on XML
<Button
Android: id = "@ + id/button5"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "button5"
Android: text = "Button"/>


Public void button5 (View v)
{
 
Toast. makeText (RE1Activity. this, "button5 ",

2000). show ();
}


------------- External listener
MButton4.setOnClickListener (LOVEYOU);} created outside brackets

OnClickListener LOVEYOU = new Button. OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Toast. makeText (RE1Activity. this, "50000 ",

2000). show ();

}

};

 

 

--------------------- Listener Method

OnClickListener LOVEYOU;

 
 

LOVEYOU = new Button. OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Toast. makeText (RE1Activity. this, "50000", 2000). show ();

}
};
MButton4.setOnClickListener (LOVEYOU );

 

 

From the column of gongzibai

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.