就简单记录一下这三种方法,不然我容易忘记,虽然挺简单了啦~~~~·每个按钮算一种方法啦
package com.example.test1;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener {
private Button bt1;
private Button bt2;
private ImageButton imagebt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 =findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"我被点击了",Toast.LENGTH_SHORT).show();
}
});
bt2 = findViewById(R.id.bt2);
bt2.setOnClickListener(new MyOnClickListenter(){
@Override
public void onClick(View v) {
super.onClick(v);
Toast.makeText(MainActivity.this,"我被点击了",Toast.LENGTH_SHORT).show();
}
});
imagebt1 = findViewById(R.id.imagebt1);
imagebt1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"我被点击了",Toast.LENGTH_SHORT).show();
}
}
class MyOnClickListenter implements View.OnClickListener {
@Override
public void onClick(View v) {
Log.i("tag","dianji");
}
}