纺纱厂和重点 [英] spinners and focus

查看:121
本文介绍了纺纱厂和重点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用的活动形式微调的一个问题。

I'm having a problem with spinners in a form activity.

我期待一个微调,以获得焦点,当用户感动,但这个似乎并没有发生。喷丝似乎只获得焦点,如果我用我的轨迹球(在Nexus One的)不同组件之间移动。

I was expecting a spinner to gain focus when a user "touched" it but this does not seem to happen. A spinner only seems to gain focus if I use my tracker ball (on a Nexus One) to move between the different components.

这是烦人,因为我使用了android:selectAllOnFocus =真的形式第一的EditText视图属性。由于纺纱从来不从的EditText组件的焦点离开它的内容始终是高新亮(这是丑陋的IMO)。

This is annoying because I'm using the android:selectAllOnFocus="true" attribute on the first EditText view in the form. Because the spinners never take focus away from the EditText component its contents are always hi-lighted (which is ugly IMO).

我已经试过用

    spinner.requestFocus();

spinner.requestFocus();

但这(貌似)没有任何影响。

but this (seemingly) has no effect.

我已经试过,要求注重微调在AdapterView.OnItemSelectedListener但他只是导致

I've tried requesting focus on the spinner in a AdapterView.OnItemSelectedListener but his just results in

窗口已经集中,忽略了聚焦增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@44cb0380

Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44cb0380

任何人都可以解释这种奇怪的行为和/或可能的方式解决它。

Can anyone explain this odd behaviour and/or possible ways around it.

非常感谢,

推荐答案

您必须使用 setFocusableInTouchMode()第一。然后你遇到一个不同的问题:你必须挖掘微调两次改变它(一次将焦点设置,然后再次看到选项列表)。我的解决方案是创建我自己的微调子类,可使从第一点触对焦增益模拟第二:

You have to use setFocusableInTouchMode() first. Then you run into a different problem: you have to tap the spinner twice to change it (once to set focus, then again to see the list of options). My solution is to create my own Spinner subclass that causes the focus gain from the first tap to simulate the second:

class MySpinnerSubclass extends Spinner {

    private final OnFocusChangeListener clickOnFocus = new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            // We don't want focusing the spinner with the d-pad to expand it in
            // the future, so remove this listener until the next touch event.
            setOnFocusChangeListener(null);
            performClick();
        }
    };

    // Add whatever constructor(s) you need.  Call 
    // setFocusableInTouchMode(true) in them.

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {

            // Only register the listener if the spinner does not already have
            // focus, otherwise tapping it would leave the listener attached.
            if (!hasFocus()) {
                setOnFocusChangeListener(clickOnFocus);
            }
        } else if (action == MotionEvent.ACTION_CANCEL) {
            setOnFocusChangeListener(null);
        }
        return super.onTouchEvent(event);
    }
}

要给予适当的信贷,我得到了我的灵感来自 Kaptkaos的回答到的这个问题

To give proper credit, I got my inspiration from Kaptkaos's answer to this question.

这篇关于纺纱厂和重点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆