Button is a very simple control in Android. It can be said to be very common in our daily projects and has a very high usage rate. The following is an example code to introduce four methods to bind events to Android studio button. The specific code is as follows:
package com.geli_2.sujie.sujiegeili2testbutton; import ; import .; import ; import ; import ; public class MainActivity extends AppCompatActivity implements { private Button mBtClick; private Button mBtClick2; private Button mBtClick3; private Button mBtClick4; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); mBtClick = (Button) findViewById(.btnTest1); //1, fdv find button id // 2. Alt Ctrl F forms a global field (new () //3. Type this line of code to automatically embed internal class method { @Override public void onClick(View v) { //4. Toast tab tab call up toast method. The above is the method one (, "hello world", Toast.LENGTH_SHORT).show(); } }); mBtClick2 = (Button) findViewById(.btnSample2); //5. Find button id fdv of button 2 (new Button_2_OnClickListener()); //9. The instantiation method is bound to the button. The above is the method two mBtClick3 = (Button) findViewById(.btnTry3); //10. Find button id fdv of button 3 (this); //11. This means main activity //12 But here is the method parameter is the onclicklistener interface, and this Alt Enter will add the implemented interface to this method. mBtClick4 = (Button) findViewById(.btnExample4); //5. Find button id fdv of button 4 } /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { //Method 3 } class Button_2_OnClickListener implements //6. Set a click method and inherit from view after naming it. // onclicklistener //7. Then implement the method on inheriting implements alt + enter { /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { //Method 2 } } public void click(View view) { //Method 4 } }
Summarize
The above are the four methods of binding events of Android studio button that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!