创建成员内部类
XML代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TextView
android:id="@+id/textview01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮 "
tools:layout_editor_absoluteX="167dp"
tools:layout_editor_absoluteY="499dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
java代码
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static com.example.demo.R.*;
public class MainActivity extends AppCompatActivity {
private TextView textview ;
private Button button;
int sum=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
textview=(TextView)findViewById(id.textview01);
textview.setText("safasfa");
button=(Button)findViewById(id.button);
button.setOnClickListener(new ButtonListener());
}
class ButtonListener implements View.OnClickListener{
public void onClick(View v){
sum++;
textview.setText("*********被点击的次数"+sum);
}
}
}
1.在XML中写入Button(为例子),并且设置其id
2.在java代码中取获取XML中的button组件(通过findViewById(xml中的组件id))
3.定义一个事件类
4.通过java代码获取到的xml组件与事件类进行绑定
创建匿名内部类
xml代码同上
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static com.example.demo.R.*;
public class MainActivity extends AppCompatActivity {
private TextView textview ;
private Button button;
int sum=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
textview=(TextView)findViewById(id.textview01);
textview.setText("safasfa");
button=(Button)findViewById(id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sum++;
System.out.println("我被点击了:"+sum+"次");
}
});
}
}
三杀
Activity直接实现监听器接口
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static com.example.demo.R.*;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textview ;
private Button button;
int sum=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
textview=(TextView)findViewById(id.textview01);
textview.setText("safasfa");
button=(Button)findViewById(id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
sum++;
System.out.println("我被点击了"+sum);
}
}
四杀
通过XML布局文件定义onClick属性
xml
只需要 android:onClick=“MyButton” (MyButton为java里面的绑定方法)
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮 "
android:onClick="MyButton" //
tools:layout_editor_absoluteX="167dp"
tools:layout_editor_absoluteY="499dp"
/>
java代码只需要有一个点击事件的方法即可
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.demo.R.*;
public class MainActivity extends AppCompatActivity {
private TextView textview ;
private Button button;
int sum=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
}
public void MyButton(View v){
Toast.makeText(MainActivity.this,"safdsf",Toast.LENGTH_SHORT).show();
}
}
多个按钮使用同一个监听
创建内部类接口
onclick方法中通过switch(R.id.xxx)方式区别被点击的按钮是哪一个
之后在对相应按钮进行事件处理
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.demo.R.*;
public class MainActivity extends AppCompatActivity {
private TextView textview ;
private Button button1;
private Button button2;
int sum=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
button1=findViewById(id.button);
button2=findViewById(id.button2);
Buttons bnl=new Buttons();
button1.setOnClickListener(bnl);
button2.setOnClickListener(bnl);
}
public class Buttons implements View.OnClickListener{
public void onClick(View v){
switch (v.getId()){
case id.button:
Toast.makeText(MainActivity.this,"实现第一个按钮",Toast.LENGTH_SHORT).show();
break;
case id.button2:
Toast.makeText(MainActivity.this,"实现第二个按钮",Toast.LENGTH_SHORT).show();
break;
}
}
}
}