Android Program Development: (9) User Interface -- 9.3 register event listener

Source: Internet
Author: User

When you interact with views, views also trigger events. For example, when a user clicks a button, you need to serve the event. Only in this way can you perform some appropriate actions. To do this, you need to register a listener for the view views.

Using the example in the previous section, there are two buttons in the activity. We can use an anonymous class to set click events for the button.

[Java]
Package net. learn2develop. UIActivity;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;
 
Public class UIActivityActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

// --- The two buttons are wired to the same event handler ---
Button btn1 = (Button) findViewById (R. id. btn1 );
Btn1.setOnClickListener (btnListener );

Button btn2 = (Button) findViewById (R. id. btn2 );
Btn2.setOnClickListener (btnListener );
}

// --- Create an anonymous class to act as a button click listener ---
Private OnClickListener btnListener = new OnClickListener ()
{
Public void onClick (View v)
{
Toast. makeText (getBaseContext (),
(Button) v). getText () + "was clicked ",
Toast. LENGTH_LONG). show ();
}
};

@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event)
{
Switch (keyCode)
{
Case KeyEvent. KEYCODE_DPAD_CENTER:
Toast. makeText (getBaseContext (),
"Center was clicked ",
Toast. LENGTH_LONG). show ();
Break;
Case KeyEvent. KEYCODE_DPAD_LEFT:
Toast. makeText (getBaseContext (),
"Left arrow was clicked ",
Toast. LENGTH_LONG). show ();
Break;
Case KeyEvent. KEYCODE_DPAD_RIGHT:
Toast. makeText (getBaseContext (),
"Right arrow was clicked ",
Toast. LENGTH_LONG). show ();
Break;
Case KeyEvent. KEYCODE_DPAD_UP:
Toast. makeText (getBaseContext (),
"Up arrow was clicked ",
Toast. LENGTH_LONG). show ();
Break;
Case KeyEvent. KEYCODE_DPAD_DOWN:
Toast. makeText (getBaseContext (),
"Down arrow was clicked ",
Toast. LENGTH_LONG). show ();
Break;
}
Return false;
}
 
}
If you click OK or Cancel, the corresponding message will appear on the screen, which means that the appropriate event is triggered.


In addition to defining an anonymous class, you can also define an anonymous internal class to process events. The following example shows how to handle the onFocusChange () method of EditText.

[Java]
// --- Create an anonymous inner class to act as an onfocus listener ---
EditText txt1 = (edittext=findviewbyid(r.id.txt 1 );
Txt1.setOnFocusChangeListener (new View. OnFocusChangeListener ()
{
@ Override
Public void onFocusChange (View v, boolean hasFocus ){
Toast. makeText (getBaseContext (),
(EditText) v). getId () + "has focus-" + hasFocus,
Toast. LENGTH_LONG). show ();
}
});
When EditText gets the focus, a message is printed on the screen.


You can also use an anonymous internal class to set listeners for two buttons.

[Java]
// --- The two buttons are wired to the same event handler ---
Button btn1 = (Button) findViewById (R. id. btn1 );
// Btn1.setOnClickListener (btnListener );
Btn1.setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// --- Do something ---
}
});
 
Button btn2 = (Button) findViewById (R. id. btn2 );
// Btn2.setOnClickListener (btnListener );
Btn2.setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
// --- Do something ---
}
});

So which method should I use to set a listener for the view? When you need to use a listener to serve multiple views, the anonymous class is very useful. If you only need to set a listener for a single view, use the anonymous internal class.

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.