onTouchListener切换按钮,忽略第一个preSS? [英] onTouchListener togglebutton, ignores first press?

查看:105
本文介绍了onTouchListener切换按钮,忽略第一个preSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该运行code,当我preSS下来,更code,当我放手一切换按钮。但是我第一次preSS和放手没有任何反应。每隔一次是罚款,这是为什么?我可以看到该方法只运行,当我松开按钮的第一次(不触发方法的任何onTouch部分虽然),我怎么能解决这个问题,并将它的第preSS工作?

I have a togglebutton that should run code when I press it down and more code when I let go. However the first time I press and let go nothing happens. Every other time it is fine, why is this? I can see the method only runs the first time when I let go of the button (it does not trigger any onTouch part of the method though), how can I get around this, and have it work for the first press?

public void pushtotalk3(final View view) {
    ((ToggleButton) view).setChecked(true);
    ((ToggleButton) view).setChecked(false);
    view.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //if more than one call, change this code
            int callId = 0;
            for (SipCallSession callInfo : callsInfo) {
                callId = callInfo.getCallId();
                Log.e(TAG, "" + callInfo.getCallId());
            }
            final int id = callId;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {  //press
                    ((ToggleButton) view).setBackgroundResource(R.drawable.btn_blue_glossy);
                    ((ToggleButton) view).setChecked(true);
                    OnDtmf(id, 17, 10);
                    OnDtmf(id, 16, 9);
                    return true;
                }
                case MotionEvent.ACTION_UP: { //release
                    ((ToggleButton) view).setBackgroundResource(R.drawable.btn_lightblue_glossy);
                    ((ToggleButton) view).setChecked(false);
                    OnDtmf(id, 18, 11);
                    OnDtmf(id, 18, 11);
                    return true;
                }
                default: return false;
            }
        }
    });
}

编辑:在XML的按钮:

the xml for the button:

<ToggleButton
        android:id="@+id/PTT_button5"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:text="@string/ptt5" 
        android:onClick="pushtotalk5"
        android:layout_weight="50"
        android:textOn="Push To Talk On"
        android:textOff="Push To Talk Off"
        android:background="@drawable/btn_lightblue_glossy"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        />      

编辑:硬件问题,无法测试解决方案ATM

hardware problem, can't test solutions atm.

推荐答案

in_call_card.xml 包含切换按钮 。据消息人士透露code的环节, in_call_card.xml InCallCard.java 充气。 InCallCard 是由应用程序定义自定义视图。

Your in_call_card.xml contains the ToggleButton. According to the link of the source code, the in_call_card.xml is inflated in InCallCard.java. InCallCard is a custom view defined by the application.

为了附加的监听器,你需要看到的 InCallCard 正在内的 InCallActivity 使用。你会发现它在 getView 办法(靠近文件末尾)。

In order to attach the listener, you need to see where the InCallCard is being used within the InCallActivity. And you find it in the getView method (near the end of the file).

根据以上的观察,您的问题可以通过这样解决:

Based on the above observations, your problem can be resolved by:

1)找到 InCallCard 视图中的按钮

1) find the button within the InCallCard view

2)安装 OnTouchListener 编程因为我已经如下图所示:

2) Attach the OnTouchListener programatically as I have shown below:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = new InCallCard(InCallActivity.this, null);
    }

    if(convertView instanceof InCallCard) {
        InCallCard vc = (InCallCard) convertView;
        vc.setOnTriggerListener(InCallActivity.this);

        // set the touch listener here.. 
        // (1) get the button within the InCallCard view (vc)
        // (2) set the onTouchListener
        final View view = vc.findViewById(R.id.PTT_button5);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //if more than one call, change this code
                int callId = 0;
                for (SipCallSession callInfo : callsInfo) {
                    callId = callInfo.getCallId();
                    Log.e(TAG, "" + callInfo.getCallId());
                }
                final int id = callId;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {  //press
                        ((ToggleButton) view).setBackgroundResource(R.drawable.btn_blue_glossy);
                        ((ToggleButton) view).setChecked(true);
                        OnDtmf(id, 17, 10);
                        OnDtmf(id, 16, 9);
                        return true;
                    }
                    case MotionEvent.ACTION_UP: { //release
                        ((ToggleButton) view).setBackgroundResource(R.drawable.btn_lightblue_glossy);
                        ((ToggleButton) view).setChecked(false);
                        OnDtmf(id, 18, 11);
                        OnDtmf(id, 18, 11);
                        return true;
                    }
                    default: return false;
                }
            }
        });

        SipCallSession session = (SipCallSession) getItem(position);
        vc.setCallState(session);
    }

    return convertView;
}

请注意:这不是做事情的最有效的方式(因为我们做一个 findViewById ),但肯定会解决您的问题。如果你想提高,了解android系统中的 ViewHolder 模式,在这里使用它。

Note: This is not the most efficient way of doing things (as we do a findViewById), but will sure resolve your problems. If you want to improve, read about the ViewHolder pattern in android and use it here.

此外,你应该已经张贴了的src code链接前面。在这里每个人都给出正确的答案,但没有人知道你的特殊情况是什么。

Also, you should have posted the src code link earlier. Everyone here was giving the right answer but no one was sure of what your particular scenario was.

这篇关于onTouchListener切换按钮,忽略第一个preSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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