標籤:匿名內部類 gif view strong ace interface 事件監聽 instance undle
一、事件三要素:
事件來源:事件發生的來源
事件:行為(點擊、觸摸…)
事件監聽器:當事件發生時,所要做的事情
二、OnClickListener (單擊事件)
介面定義: Public static interface View . OnClickListener {
Public void OnClick (View view);
}
1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.focus); 4 Button bt=(Button)super.findViewById(R.id.focusbt1); 5 //設定監聽器——>事件處理(View.OnClickListener) 6 OnClickListener ocl=new OnClickListenerImpl(); 7 bt.setOnClickListener(ocl); 8 } 9 //內部類10 public class OnClickListenerImpl implements OnClickListener{11 public void onClick(View v) {12 String str=etname.getText().toString();13 tv.setText(str);14 }15 }單擊事件——範例程式碼
1 Button bt=(Button)super.findViewById(R.id.focusbt1);2 //設定監聽器,匿名內部類3 bt.setOnClickListener(new OnClickListener () {4 public void onClick(View v) {5 String str=etname.getText().toString();6 tv.setText(str);7 }8 }); 範例程式碼(匿名內部類)
事件處理【安卓4】——單擊事件