android TouchMode下的focus问题

来源:互联网 发布:如何练硬笔书法知乎 编辑:程序博客网 时间:2024/06/05 04:34

今天试跑了"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);        }       });     }  }  


原创粉丝点击