Android版的焦点侦听和关于ImageView的点击监听器监听器、焦点、Android、ImageView

2023-09-06 11:05:16 作者:⊙o⊙

我有一个ImageView的 - 它既有属性 - 可聚焦和 focusableintouchmode 设置为真正

I have an imageview - It has both the attributes -focusable and focusableintouchmode set to true

<ImageView
        android:id="@+id/ivMenu01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="true"
        android:focusableInTouchMode="true" >
    </ImageView>

我已经实现在 onFocusChangeListener 在我的活动 -

 @Override
public void onFocusChange(View v, boolean hasFocus) {
    switch (v.getId()) {
    case R.id.ivMenu01:

            if (hasFocus) {
                ivMenu01.setImageBitmap(Utility
                        .getBitmap("Home_ford_focus.png")); // Focussed image
            } else {
                ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford.png")); // Normal image
            }

        break;

    default:
        break;
    }

}

另外在 onClickListener -

Also the onClickListener -

 case R.id.ivMenu01:
                ivMenu01.requestFocus();
                Intent iFord = new Intent(HomeScreen.this, FordHome.class);
                startActivity(iFord);

break;

现在,当我点击ImageView的第一次点击使焦点的ImageView和第二次点击执行的操作。我不知道为什么会这样。 第一次单击应该要求重点以及执行的操作。如何做到这将是非常美联社preciated任何帮助。

Now when i click the ImageView the first click gives the focus to the ImageView and the second click performs the action. I am not sure why this is happening . The first click should request focus as well as perform the action. Any help on how to do this will be highly appreciated.

推荐答案

这是小部件框架的设计方式。

It's the way the widget framework is designed.

当你看 View.onTouchEvent() code,你会发现,只有当该视图具有的所采取的点击动作进行专注的:

When you look at View.onTouchEvent() code, you'll find out that the click action is performed only if the view has taken focus:

    // take focus if we don't have it already and we should in
    // touch mode.
    boolean focusTaken = false;
    if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
        focusTaken = requestFocus();
    }

    if (!mHasPerformedLongPress) {
        // This is a tap, so remove the longpress check
        removeLongPressCallback();

        // Only perform take click actions if we were in the pressed state
        if (!focusTaken) {
            // click
        }
    }

因此​​,当你注意到了,第一次点击,使视图获取焦点。第二个将触发click处理程序,因为视图已经拥有焦点。

So, as you noticed, the first click makes the view gain focus. The second one will trigger the click handler since the view already has focus.

如果你想修改的ImageView的位图时,它的 pressed 的,你应该实施 View.OnTouchListener 并通过设置ImageView.setOnTouchListener()方法。该侦听器应该或多或少是这样的:

If you want to alter the bitmap of the ImageView when it's pressed, you should implement an View.OnTouchListener and set it via ImageView.setOnTouchListener() method. That listener should look more or less like this:

private View.OnTouchListener imageTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // pointer goes down
            ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford_focus.png"));
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            // pointer goes up
            ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford.png"));
        }
        // also let the framework process the event
        return false;
    }
};

您还可以使用选择又名状态列表绘制对象来实现同样的事情。看到这里引用:http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

You can also use a Selector aka State List Drawable to achieve the same thing. See reference here: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList