我们在电视或者手机上开发的时候,经常会遇到如下情况,在屏幕上布局了多个TextView,ImageView,ImageButton等,但是在使用键盘的方向键操作的时候只有Button,ImageButton才能有焦点,但是textview或者imageview确不能自动获得焦点
如上图所示通过键盘操作方向键的时候,只有两个button才能自动获得焦点,布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_as_learning_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.migu.hwj.as.leanring.AsLearningMainActivity">
<TextView
android:id="@+id/tv_process_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App Name"/>
<TextView
android:text="Service Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_process_name"
android:layout_alignParentLeft="true"
android:id="@+id/tv_service_name"/>
<Button
android:text="打开activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_show_activity"
android:layout_below="@+id/tv_service_name"/>
<Button
android:text="打开Service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_start_service"
android:layout_below="@+id/btn_show_activity"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_start_service"
android:layout_alignEnd="@+id/tv_process_name"
android:layout_marginEnd="13dp"
android:layout_marginTop="75dp"
android:background="@drawable/common_google_signin_btn_text_dark_disabled"
android:id="@+id/imageView"/>
</RelativeLayout>
出现这个问题的原因是什么尼?
其实就是在Button,textview,imageview,imagebutton的构造函数导致的
我们先看下Button的构造函数
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
在这个构造函数里面,Button使用了com.android.internal.R.attr.buttonStyle
那我们该如何查找com.android.internal.R…的资源尼
com.android.internal.R 中的资源在 frameworks/base/core/res,既然我们查看的是attr.buttonStyle,自然需要到frameworks/base/core/res/values下去查看attrs.xml文件;打开attrs.xml可见
<!-- Normal Button style. -->
<attr name="buttonStyle" format="reference" />
这个表示buttonStyle引用了另外一个id,这些属性具体的值是在values的themes.xml文件中设置的,我们打开themes.xml,查找buttonStyle
<!-- Button styles -->
<item name="buttonStyle">@style/Widget.Button</item>
<item name="buttonStyleSmall">@style/Widget.Button.Small</item>
可以看见buttonStyle使用了@style/Widget.Button,接着查看styles.xml中的Widget.Button
<style name="Widget.Button">
<item name="background">@drawable/btn_default</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="textAppearance">?attr/textAppearanceSmallInverse</item>
<item name="textColor">@color/primary_text_light</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>
在这个style中,使用了@drawable/btn_default,这时我们需要去frameworks\base\core\res\res\drawable目录下查看btn_default
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/btn_default_normal_disable" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="@drawable/btn_default_normal_disable_focused" />
<item
android:drawable="@drawable/btn_default_normal_disable" />
</selector>
由此可见,有了如上操作,Button当操作键盘方向的时候,才有了焦点;
同样的ImageButton的构造方法如下
public ImageButton(Context context) {
this(context, null);
}
public ImageButton(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.imageButtonStyle);
}
正是由于使用了imageButtonStyle才使得可以自动获得焦点
然后ImageView继承自View,且其构造函数为,
public ImageView(Context context) {
super(context);
initImageView();
}
public ImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ImageView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public ImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
因此不具备操作键盘,使其获得焦点的能力;TextView也是同理;
那我们如何让ImageView,TextView在操作键盘方向的时候,也可以自动滑动焦点尼?通常我们需要重写这两个类,在代码里面实现方式如下,当然如果使用xml,则更简单,只要使用style:="…"即可
public class MiGuTvImageView extends ImageView {
private Context mContext;
//不同状态下的按钮形态
public static int[] mImgNormalState = new int[] {};
public static int[] mFocus = new int[] {android.R.attr.state_focused,android.R.attr.state_enabled};
public static int[] mEnable = new int[] {android.R.attr.state_enabled};
public static int[] mFocusOnly = new int[] {android.R.attr.state_focused};
public static int[] mPress = new int[] {android.R.attr.state_pressed};
public MiGuTvImageView(Context context, int type) {
super(context);
// TODO Auto-generated constructor stub
setClickable(true);
setFocusable(true);
setBgState(context);
}
private void setBgState(Context c) {
mContext = c;
StateListDrawable bg = new StateListDrawable();
Drawable pressedDrawable = mContext.getDrawable(R.drawable.ic_launcher);
Drawable normalDrawable = mContext.getDrawable(R.drawable.tv_ad);
bg.addState(mFocusOnly, pressedDrawable);
bg.addState(mPress, pressedDrawable);
bg.addState(mImgNormalState, normalDrawable);
setBackgroundDrawable(bg);
}
}