按钮监听事件的实现方式:
1.匿名内部类;
2.匿名外部类;
3.实现接口;
代码 :
package com.example.first;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button loginButton;
private ImageButton imagBt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginButton = (Button)findViewById(R.id.button1);
imagBt= (ImageButton) findViewById(R.id.imageButton1);
/*loginButton.setOnClickListener(new OnClickListener() {//第一种
@Override
public void onClick(View v) {
System.out.println("我的Button被点了");
}
});*/
loginButton.setOnClickListener(new MyOnClickListener(){//第二种
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
super.onClick(v);
Toast.makeText(MainActivity.this, "loginButton要执行的罗辑", 1).show();
}
});
imagBt.setOnClickListener(this);
}
@Override
public void onClick(View v) {//第三种
Log.i("tag", "第三种方式实现");
}
}
class MyOnClickListener implements OnClickListener
{
@Override
public void onClick(View v) {
v.setAlpha(0.5f);
}
}