Button is an important control used by the program to interact with users. I believe you are very familiar with this control. It is one of the most commonly used controls. Since there is a button, there must be an onClick method. Here are three methods to realize click events. You can choose a way you like to write.
We first place a Button control in the layout file. It is very simple to center it horizontally and vertically. The text in the Button is also centered and aligned. The font size is 15sp and the content is "I am a Button". The specific code is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ButtonActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:gravity="center" android:text="I'm the button" android:textSize="15sp" /> </RelativeLayout>
Run the simulator as follows:
Next, let's implement the button click event: as long as we click the button, a Toast prompt message will pop up with the content of "button clicked".
1, Anonymous inner class implementation |
---|
Declare and bind the control in the onCreate() method, then register the listener and override the onClick() method. Just add the logic to be processed in the onClick() method.
Here we only use Toast to display a message. All 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,"The button was clicked",Toast.LENGTH_SHORT).show(); } }); } }
In this way, as long as we click the button, the onClick() method in the listener will be executed. The effect is as follows:
2, Interface implementation |
---|
The second method only needs to reference the View.OnClickListener interface, and then button = findviewbyid (r.id.button); Used to declare and bind button controls, button.setOnClickListener(this); Set the listener of button, both of which are indispensable. The following is to rewrite the onClick() method. switch statements are generally used. The parameter is view. Different click events can be given according to different IDs. There is no need to set click events separately for each button like the anonymous internal class above. All 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,"The button was clicked",Toast.LENGTH_SHORT).show(); default: break; } } }
Let's see the effect:
3, Layout implementation |
---|
The above two click events are very simple. I believe you have mastered them, so the next method is simpler.
In the layout file, we set the attribute android:onClick="onClick" for each Button used. The others do not need to be changed.
Then we write an onClick() method in ButtonActivity, which is not rewritten here, because we do not inherit any parent class and 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,"The button was clicked",Toast.LENGTH_SHORT).show(); } }
Let's run the program and find that there is a response after clicking the button.
Well, the above are the three click events to realize Android buttons. Have you learned them. If you can learn it, point to three companies to support the blogger.