按钮同时处于选中状态和聚焦状态

时间:2019-07-09 14:15:04

标签: android user-interface button focus

我有4个按钮。与聚焦状态相比,背景空间要大一些。如下屏幕所示。在改变焦点时,文字颜色应该改变。 当单击任何按钮时,背景应该改变并且焦点应该照常工作,除了白色矩形不应像其他普通按钮一样留在被单击的按钮内

Result

当焦点移动时,只有在白色的矩形内移动并且背景保持不变。

UI:          

    <com.example.mytvapplicationjava.MyButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <com.example.mytvapplicationjava.MyButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />

    <com.example.mytvapplicationjava.MyButton
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" />

    <com.example.mytvapplicationjava.MyButton
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 4" />
</LinearLayout>

MyButton.java

public class MyButton extends android.support.v7.widget.AppCompatButton {
    public MyButton(Context context) {
        super(context);
        init();
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setFocusable(true);
        setFocusableInTouchMode(true);
        setClickable(true);
        setGravity(Gravity.CENTER);
        setBackground(getResources().getDrawable(R.drawable.tab_item_selector));
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        Log.d("Basava", "onFocusChanged focused? " + focused + " isSelected? " + isSelected());
        if (focused) {
            setTextColor(getResources().getColor(android.R.color.black));
        } else {
            setTextColor(getResources().getColor(android.R.color.white));
        }
    }
}

tab_item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/layers_selected" android:state_selected="true" /> <!-- selected -->
    <item android:drawable="@drawable/layers_focused" android:state_focused="true" /> <!-- focused -->
    <item android:drawable="@drawable/layers_focused" android:state_hovered="true" /> <!-- hovered -->

    <item android:drawable="@drawable/layers" /> <!-- default -->

</selector>

layers.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="150dp"
        android:height="70dp">
        <color android:color="@color/lb_grey" />
    </item>
    <item
        android:width="100dp"
        android:height="50dp"
        android:gravity="center">
        <color android:color="@color/lb_tv_white" />
    </item>
</layer-list>

layers_focused.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="150dp"
        android:height="70dp">
        <color android:color="@color/lb_grey" />
    </item>
    <item
        android:width="100dp"
        android:height="50dp"
        android:gravity="center">
        <color android:color="@android:color/white" />
    </item>
</layer-list>

layers_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="150dp"
        android:height="70dp">
        <color android:color="@android:color/holo_orange_dark" />
    </item>
    <item
        android:width="100dp"
        android:height="50dp"
        android:gravity="center">
        <color android:color="@android:color/white" />
    </item>
</layer-list>

0 个答案:

没有答案