In the first two articles, we introduced how to use internal classes and anonymous internal classes to implement button clicks. The link is below:
Use internal classes to implement button click events
Use anonymous internal classes to implement button click events
**Today we understand the remaining two common implementation methods:
- How to implement button click event through activity
- How to realize button click event through XML binding button **
===================================== ================================================= ================
One: realize the button click event through activity, do
n’t talk nonsense, just go to the JAVA code
public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {
@Override
public void onClick(View v) {
Log.e("TGA","通过activity实现的按钮点击事件");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
// 绑定Button 对象
Button button = findViewById(R.id.btn4);
button.setOnClickListener(this);
}
}
================================================= ================================================= =====
Detailed code:
First of all, we can find out whether this way of implementation is to implement an interface called OnClickListener in the activity~ Then implement a method of onClick() in the interface, right?
@Override
public void onClick(View v) {
Log.e("TGA","通过activity实现的按钮点击事件");
}
Then we can think about the previous inner class that implements the click event and the anonymous inner class that implements the click event. Didn’t they essentially implement the OnClickListener interface~ It’s just that the inner class implementation is to create an inner class to implement the interface, anonymous The inner class is to go to a new interface in the listener, right? , Then since we are trying our best to implement the OnClickListener interface, then activity is not a ready-made class, of course we can use it to directly implement this interface, and then as long as the listener detects that the button is clicked, it will go To implement the method onClick in the OnClickListener interface, we still print an error log on the console as usual: "Button click event implemented by activity" If this sentence appears, it proves that we have completed this implementation method!
=====================================================================
We first bind an id to the button to let java know which button we clicked.
Button button = findViewById(R.id.btn4); //绑定id
Then we add a listener to the button. When the listener detects that our button has a click action, it will execute the onClick() method of the object corresponding to the "parameter".
button.setOnClickListener(this);
Very good, knowing this, let's think about whether this interface is implemented in our current activity? So can we pass in the activity object in the parameter? Of course it can~ do we need a new object? do you need? My great java will definitely not make you so hard, don't forget! this keyword! Does this represent the current class object? Well, let's pass this in, isn't this done!
So far, congratulations to the guest officer for completing a button click event method-realize the button click event through activity
================================================= ================================================= ====
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ButtonActivity">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义内部类的方法实现的点击事件"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义内部类的方法实现的点击事件2"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="匿名内部类的方法实现的点击事件"/>
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过activity实现的点击事件"/>
<Button
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myClick"
android:text="在xml文件中绑定实现点击事件"/>
<Button
android:id="@+id/btn6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myClick"
android:text="在xml文件绑定实现点击事件2"
/>
</LinearLayout>
Let's see the effect: ![Insert picture description here](https://img-blog.csdnimg.cn/20200915224357817.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01yX21famlhX2Jhbw==,size_16,color_FFFFFF,t_70#pic_center)
we click the fourth button to see the effect:
![Insert picture description here](https://img-blog.csdnimg.cn/2020091522473922.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01yX21famlhX2Jhbw==,size_16,color_FFFFFF,t_70#pic_center)
ok our log is printed out on the console, complete
Two, realize the button click event through XML binding
Let's take a look at how to implement button click events through XML binding
This time we first look at the xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ButtonActivity">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义内部类的方法实现的点击事件"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义内部类的方法实现的点击事件2"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="匿名内部类的方法实现的点击事件"/>
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过activity实现的点击事件"/>
<Button
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myClick"
android:text="在xml文件中绑定实现点击事件"/>
<Button
android:id="@+id/btn6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myClick"
android:text="在xml文件绑定实现点击事件2"
/>
</LinearLayout>
First of all, we can see that the button that realizes the click event through the xml file binding in the xml code is compared with several other buttons, and we can find that it has one more statement than other buttons:
android:onClick="myClick"
What is this for? What's the effect? To put it bluntly, when you click this button, he will implement a method called myClick. What is the method of this myClick? I believe you have guessed it, yes, it is a method we wrote ourselves in the activity. First understand this, let's take a look at the activity code to see how sacred this method is!
java code:
public class ButtonActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
}
//在xml中定义的myClick方法
public void myClick(View view){
Log.e("TAG","通过xml绑定实现的点击事件");
}
}
We found that besides this myClick method, there is no listener, no button id bound, no object creation, no interface implementation, no internal class, anonymous internal class, it is just a myClick method defined in xml, come, Let’s take a closer look at whether this method is like the onClick method in the OnClickListener interface.
The onClick method in the OnClickListener interface
@Override
public void onClick(View v) {
Log.e("","");
}
The myClick method we created ourselves:
public void myClick(View view){
Log.e("","");
}
Are there any similarities? Let's take a look at the mechanism of the method in this xml.
First of all, we added onClick to the xml. This function is to execute the corresponding method in the activity if the current button is clicked.
So, there is no need for the listener, and there is no need to implement this interface. The purpose of implementing the interface is not to implement it. The onClick method~ Then we can define a method for him to implement it. They all have the same parameter type View, so that it can replace the onClick method. When we click the button, it will execute the method we wrote.
Finally, we look at the running effect:
Let's click on the underlined button to see the effect:
a prompt is successfully output in the console, so that the button click event through XML binding is also complete~ The
button click event through XML binding is basically over, I will be in the next article Talk about how to implement multiple click events in the xml binding button
Of course, the realization of click events is not limited to these four, but these four are quite common.
Use internal classes to implement button click events
Use anonymous internal classes to implement button click events
================================================= ================================================= ====
Finally, thank the guest officer for insisting on reading this low-tech and long-winded blog.
If there is anything wrong, please point it out~ Thank you!
Finally, mutual encouragement: The road to success is full of thorns and bumps. Only I know those sadness and tears, but I know that persistence must be cool!
Ending Sapphire~~
Trailer: Next: How to use XML button binding to achieve multiple click events and a summary of these four button click events~