package com.example.daike;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.PersistableBundle;
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 ActionBarActivity implements OnClickListener {
/*
* 监听BUTTon类
* 匿名监听登录这个按钮BUTTON类
*/
private Button loginButton;
private ImageButton imBt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seclayout);
loginButton = (Button) findViewById(R.id.login_button);
imBt=(ImageButton) findViewById(R.id.imageButton1);
// loginButton.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View arg0) {
// Toast.makeText(MainActivity.this,"你点击了登陆按钮", Toast.LENGTH_LONG).show();
//
// }
// });
//独立模块监听
loginButton.setOnClickListener(listener);
//接口模式监听
imBt.setOnClickListener(this);
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("tag","登录事件监听成功");
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
// if (id == R.id.action_settings) {
// return true;
// }
// return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(MainActivity.this, "你点击了设置", Toast.LENGTH_LONG).show();
break;
case R.id.action_settingss:
Toast.makeText(MainActivity.this, "你点击了设置2", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i("tag", "图片按钮监听成功");
}
}
这是我学习的内容,学会了三种监听方式,一个是匿名监听以下形式的
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this,"你点击了登陆按钮", Toast.LENGTH_LONG).show();
}
});
独立模块的监听事件:
loginButton.setOnClickListener(listener);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("tag","登录事件监听成功");
}
};
还有通过接口的方式进行的监听,需要在这个类上添加implements OnClickListener 接口属性
eclipse会自动提示你添加这个类中的onclick方法,需要往这个按
imBt.setOnClickListener(this);
参数中传递自身的类,就可以调用这个方法,在这个方法中写明执行步骤就可以了,收获很大呢