ImageButton的焦点和事件处理
import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageButton;import android.widget.TextView;/** * ImageButton的焦点和事件处理 * */public class ActivityMain extends Activity {private ImageButton myImageButton ;private Button myButton;private TextView myTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建三个组件 myImageButton = (ImageButton)findViewById(R.id.myImageButton); myButton = (Button)findViewById(R.id.myButton); myTextView = (TextView)findViewById(R.id.myTextView1); //通过OnFocusChangeListener来响应ImageButton的onFocus事件 myImageButton.setOnFocusChangeListener(new ImageButton.OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { //如果ImageButton得到焦点onFocus则改变ImageButton的图片 if(hasFocus==true) { //更改状态说明 myTextView.setText("图片按钮状态为:Got Focus"); //更改按钮背景图片 myImageButton.setImageResource(R.drawable.iconfull); } else { myTextView.setText("图片按钮状态为: Lost Focus"); myImageButton.setImageResource(R.drawable.iconempty); } } }); //通过onClickListener来响应ImageButton的onClick事件 myImageButton.setOnClickListener(new ImageButton.OnClickListener(){ public void onClick(View v) { //如果ImageButton状态为onClick则改变ImageButton的图片和说明 myTextView.setText("图片按钮状态为: Got Click"); myImageButton.setImageResource(R.drawable.iconfull); } }); //通过onClickListener来响应Button的onClick事件 myButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { //如果Button状态为onClick改变ImageButton图片 myTextView.setText("图片按钮状态为:Lost Focus"); myImageButton.setImageResource(R.drawable.iconempty); } }); }}