packagecom.example.button;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.View.OnTouchListener;importandroid.widget.Button;importandroid.widget.ImageButton;importandroid.widget.TextView;publicclassMainActivityextendsActivity{privateTextViewtv;privateButtonbtn_ok,btn_cancle;privateImageButtonbtn_img;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv=(TextView)findViewById(R.id.tv);btn_ok=(Button)findViewById(R.id.btn_ok);btn_cancle=(Button)findViewById(R.id.btn_cancle);btn_img=(ImageButton)findViewById(R.id.btn_img);// 内部类 实现 监听btn_img.setOnTouchListener(newOnTouchListener(){@OverridepublicbooleanonTouch(Viewv,MotionEventevent){// TODO Auto-generated method stubif(event.getAction()==MotionEvent.ACTION_DOWN){btn_img.setBackgroundDrawable(getResources().getDrawable(R.drawable.face1));}elseif(event.getAction()==MotionEvent.ACTION_UP){btn_img.setBackgroundDrawable(getResources().getDrawable(R.drawable.blank));}returnfalse;}});// 也可以 通过implements OnClickListener 实现监听btn_ok.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){// TODO Auto-generated method stubif(v==btn_ok){tv.setText("触发确定按钮事件");}}});// 内部类 实现监听器btn_cancle.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){// TODO Auto-generated method stubif(v==btn_cancle){tv.setText("触发取消按钮事件");}}});}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main,menu);returntrue;}}