android TouchMode下的focus问题

今天试跑了"Google Android SDK开发范例大全源码里的EX04_02工程",发现mImageButton1.setOnFocusChangeListener这个监听器木有反应。查了很多资料发现原因是:在Android TouchMode模式下是不存在focus的。所谓的focus是指被选中的意思,类似于windows平台下单击一个文件夹后鼠标的焦点(focus)就在该文件夹上面。既然不存在focus,也就不存在FocusChange的问题,所以setOnFocusChangeListener监听器不起作用也就可以理解。如果想在TouchMode下面使用这个监听器可以使用如下两种方法:

         1.增加一句代码:mImageButton1.setFocusableInTouchMode(true)

         2.在main.xml中mImageButton标签中加入一个子标签:android:focusableInTouchMode="true"。这两种方法的作用都是使得focus在TouchMode下恢复作用。

       关于TouchMode可以参考下面两片文章:

       1.http://developer.android.com/resources/articles/touch-mode.html

       2.http://hi.baidu.com/qmiao128/blog/item/3bc40f54e0605144574e0048.html

         下面附上Google Android SDK开发范例大全源码里的EX04_02工程源代码

public class EX04_02 extends Activity   
{  
  /*声明三个对象变量(图片按钮,按钮,与TextView)*/  
  private ImageButton mImageButton1;  
  private Button mButton1;  
  private TextView mTextView1;  
    
  /** Called when the activity is first created. */  
  @Override  
  public void onCreate(Bundle savedInstanceState)  
  {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
      
    /*通过findViewById构造三个对象*/  
    mImageButton1 =(ImageButton) findViewById(R.id.myImageButton1);  
    mButton1=(Button)findViewById(R.id.myButton1);  
    mTextView1 = (TextView) findViewById(R.id.myTextView1);  
      
    /*通过OnFocusChangeListener来响应ImageButton的onFous事件*/  
    mImageButton1.setOnFocusChangeListener(new OnFocusChangeListener()  
    {  
      public void onFocusChange(View arg0, boolean isFocused)  
      {  
        // TODO Auto-generated method stub  
          
        /*若ImageButton状态为onFocus改变ImageButton的图片 
         * 并改变textView的文字*/  
        if (isFocused==true)  
        {  
          mTextView1.setText("图片按钮状态为:Got Focus");  
          mImageButton1.setImageResource(R.drawable.iconfull);  
        }  
        /*若ImageButton状态为offFocus改变ImageButton的图片 
         *并改变textView的文字*/  
        else   
        {  
          mTextView1.setText("图片按钮状态为:Lost Focus");  
          mImageButton1.setImageResource(R.drawable.iconempty);  
        }  
      }  
    });  
         
    /*通过onClickListener来响应ImageButton的onClick事件*/  
    mImageButton1.setOnClickListener(new OnClickListener()  
    {  
      public void onClick(View v)  
      {  
        // TODO Auto-generated method stub  
        /*若ImageButton状态为onClick改变ImageButton的图片 
         * 并改变textView的文字*/  
        mTextView1.setText("图片按钮状态为:Got Click");  
        mImageButton1.setImageResource(R.drawable.iconfull);  
      }     
    });  
        
    /*通过onClickListener来响应Button的onClick事件*/  
    mButton1.setOnClickListener(new OnClickListener()  
    {  
      public void onClick(View v)  
      {  
        // TODO Auto-generated method stub  
        /*若Button状态为onClick改变ImageButton的图片 
         * 并改变textView的文字*/  
        mTextView1.setText("图片按钮状态为:Lost Focus");  
        mImageButton1.setImageResource(R.drawable.iconempty);  
      }   
    });   
  }  
}  


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值