ImageButton的焦点和事件处理
Java代码
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");
相关新闻>>
- 发表评论
-
- 最新评论 更多>>