Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement button click event in Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "Android how to achieve button click event", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Android how to achieve button click event" bar!

Let's first put a Button control in the layout file, which is very simple, so that it is centered horizontally and vertically, and the text in the Button is also centered, with the font size 15sp, and the content is "I am the button". The specific code is as follows:

Run the simulator, which is shown as follows:

Next, let's implement the button click event: as soon as we click the button, a Toast message pops up with the message "Button clicked".

First, anonymous inner class implementation

Declare and bind the control in the onCreate () method, then register the listener and rewrite the onClick () method, as long as you add the pending logic to the onClick () method. Here we only display one message using Toast. All the codes are as follows:

Public class ButtonActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_button); Button button=findViewById (R.id.button) Button.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View view) {Toast.makeText (ButtonActivity.this, "button clicked", Toast.LENGTH_SHORT). Show ();}});}}

In this way, as soon as we click the button, the onClick () method in the listener is executed, and the effect is as follows:

Second, interface implementation

The second method is simply to reference the interface View.OnClickListener, followed by Button button=findViewById (R.id.button); to declare and bind button controls, button.setOnClickListener (this); and to set up button listeners, both of which are indispensable.

The following is to override the onClick () method. Generally, the switch statement is used, and the parameter is view. You can assign different click events according to different id, instead of setting the click event separately for each button like the anonymous inner class above. All the codes are as follows:

Public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_button); Button button=findViewById (R.id.button); button.setOnClickListener (this) } @ Override public void onClick (View view) {switch (view.getId ()) {case R.id.button: Toast.makeText (this, "button clicked", Toast.LENGTH_SHORT) .show (); default: break;}

Let's run it to see the effect:

Third, layout realization

The above two click events are very simple, I believe you have mastered, then the next method is even simpler.

In the layout file, we set the property android:onClick= "onClick" for each Button we use, and the others do not need to be changed. Then we write an onClick () method in ButtonActivity, which is not rewritten here, because we don't have any inheriting parent class or reference interface, and the method name here can be taken at will. Then write the code logic. The complete code is as follows:

Public class ButtonActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_button);} public void onClick (View view) {Toast.makeText (this, "button clicked", Toast.LENGTH_SHORT). Show ();}}

Let's run the program and find that it also responds after clicking the button.

Thank you for your reading, the above is the content of "Android how to achieve button click event". After the study of this article, I believe you have a deeper understanding of how to achieve button click event in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report