package com.example.myapplication;
import android.content.DialogInterface;
import android.app.Activity;
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.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button mybutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//匿名内部显示类的实现
//1初始化当前所需控件
/*
* findViewById----返回的是View的对象
* */
mybutton=(Button)findViewById(R.id.button);
//监听事件通过匿名内部类
/* mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"没看到提示吗,你好无聊",Toast.LENGTH_LONG).show();
}
});*/
mybutton.setOnClickListener(new MyOnClickListener(){
@Override
public void onClick(View v) {
//调用父类MyOnClickListener的方法onClick
super.onClick(v);
Toast.makeText(MainActivity.this,"哈喽",1).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
class MyOnClickListener implements OnClickListener{
public void onClick(View v){
Log.i("tog","父类的onclick事件");
}
}