2011-03-25 61 views
0
final View b= (Button) findViewById(R.id.find); 
b.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       // TODO Auto-generated method stub 


       if(hasFocus==true) 
      { 
       Toast.makeText(user_interface.this, "onfocus", Toast.LENGTH_LONG).show(); 
      // b.setBackgroundColor(R.color.gray); 

      } 
      else 
      { 

       Toast.makeText(user_interface.this, "lossfocus", Toast.LENGTH_LONG).show(); 


      } 


      } 
     }); 

回答

0

我已经检查过它。有用。请注意,在模拟器上,当您通过滚动向上/向下箭头进入按钮时,会发生onFocus。

我有以下代码:

btnSign.setOnFocusChangeListener(new OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      // TODO Auto-generated method stub 
      if (hasFocus) { 
       //Toast line... 
      } 
     } 

    }); 

对于按钮为什么ü使用:

最终视图???可能是多数民众赞成在问题..

使用按钮b =(按钮)findViewById(R.id.find);

+0

他使用了final,否则由于anonymous类中的用法,他会得到编译错误。 – Geo 2011-03-25 08:48:11

+0

thanx,是这个工作与上/下键为什么它不适用于鼠标 – 2011-03-29 04:59:41

+0

焦点上的手机意味着焦点(光标)是在该字段上。在onFocus中未检测到鼠标光标。事实上,鼠标光标根本无法被检测到。 – 2011-03-29 08:30:56