4 years ago (2015-04-13)  Software development |   First to comment  2 
post score 0 times, average 0.0

Button listening event

In the above application screenshot we see that the game start interface uses two buttons "Start Game" and "Stop Game".Two buttons trigger the formal game and close the two actions of the game program, respectively.So bind the listener for the two button click events (OnClickListener). There are two ways to bind a listener to a button in Android: 1. In the layout file, declare a new listener in the 2 code and bind it; In the implementation of "BY fighter", I use the second, that is, create a listener in the code. Device. For the purpose of reviewing and communicating with netizens in the future, I will also introduce the first method of binding a button listener.

  • Binding file listener declared in the layout file

The declaration in the layout file is just like declaring attributes for layout. Just add in the button's attribute field: android:onClick="Button_Click" Then define the Button_Click method in the java code, define the content of the button click to trigger the event public void Button_Click (View Source) { //Supplementary code: action after clicking the button }

  • New listener in code and bind button listener

Not much to say, first code:

  This is the "By fighter" project where I bind listeners and their action codes for "start game" and "end game". First get two buttons through findViewById, through the code: button_begin.setOnClickListener(control); Set their listeners to control for both buttons as control, in the above code I define the contents of the control, in order to make two buttons share a listener , I use view.getId() to get the source ID of the clicked button, and then use the switch to select the statement to distinguish between the execution of the two buttons: startActivity(game) [enter the game activity] and System.exit(0)[ quit the current Android program ].

 

This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!

Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/android-buttons-two-ways-listen-events-2.html

Post comment

Style

No Comment

登录

Forget password?

您也可以使用第三方帐号快捷登录

切换登录

注册

TW