0

我有一个切换按钮,当我按下它时应该运行代码,当我放手时它会运行更多。但是它根本不像那样工作,就像切换按钮通常那样,我按下它并改变状态,它根本不调用触摸监听器代码。在我的 in_call_card.xml 我有:

 <ToggleButton
            android:id="@+id/PTT_button5"
            android:layout_width="0dp"
            android:layout_height="fill_parent"            
            android:text="@string/ptt5" 
            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"
            />   

在 onCreate 中,我将 setContent 设置为 in_card_main.xml,因此我必须扩展布局以使用 in_call_card.xml 中的按钮。这是我的代码,但是当我按下按钮时它永远不会运行:

LayoutInflater inflater = getLayoutInflater();
        View otherLayout = inflater.inflate(R.layout.in_call_card, null);
        final ToggleButton toggle = (ToggleButton) otherLayout.findViewById(R.id.PTT_button5);
        toggle.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                 Toast.makeText(getApplicationContext(), "woot7", Toast.LENGTH_SHORT).show();
                //may need to change to loop and send all ids.
                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:
                          toggle.setBackgroundResource(R.drawable.btn_blue_glossy);
                         toggle.setChecked(true);
                          OnDtmf(0, 17, 10);
                          OnDtmf(0, 16, 9);
                          OnDtmf(id, 17, 10);
                          OnDtmf(id, 16, 9);
                         break;
                     case MotionEvent.ACTION_UP:
                           //     Toast.makeText(getApplicationContext(), "woot", Toast.LENGTH_LONG).show();
                         //release
                         toggle.setBackgroundResource(R.drawable.btn_lightblue_glossy);
                         toggle.setChecked(false);
                         OnDtmf(0, 18, 11); 
                      OnDtmf(0, 18, 11);
                         OnDtmf(id, 18, 11);    
                      OnDtmf(id, 18, 11);       
                         break;
                   }
                   return false;
            }
        });

编辑:

in_call_main:

<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright (C) 2010 Regis Montoya (aka r3gis - www.r3gis.fr) 

    This file is part of CSipSimple.
    CSipSimple is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    If you own a pjsip commercial license you can also redistribute it
    and/or modify it under the terms of the GNU Lesser General Public License
    as an android library.

    CSipSimple is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with CSipSimple.  If not, see <http://www.gnu.org/licenses/>.
-->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ViewStub
        android:id="@+id/dropZones"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/in_call_drop_zones" />

    <LinearLayout
        android:id="@+id/inCallContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="3"
            android:orientation="horizontal" >

            <!--
                Can we do better than using two linear layouts here?
                The problem is that RelativeLayout doesn't allow to use weight and
                plays bad with hidden childs and above/below only support one id
            -->
            <!-- Grid for active calls -->

            <com.csipsimple.ui.incall.InCallInfoGrid
                android:id="@+id/activeCallsGrid"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:paddingBottom="4dip"
                android:paddingTop="4dip"
                tools:ignore="NestedWeights" />

            <!-- Grid for held calls -->

            <com.csipsimple.ui.incall.InCallInfoGrid
                android:id="@+id/heldCallsGrid"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/in_call_held_background"
                android:padding="4dip"
                android:visibility="gone" />
        </LinearLayout>

        <!-- Answer slider/buttons -->

        <com.csipsimple.ui.incall.locker.InCallAnswerControls
            android:id="@+id/inCallAnswerControls"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="2"
            android:visibility="gone" />

        <!-- Wraps split action bar manually managed -->

        <com.csipsimple.ui.incall.InCallControls
            android:id="@+id/inCallControls"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/incall_bottom_bar_bg" />
    </LinearLayout>

    <com.csipsimple.ui.incall.locker.ScreenLocker
        android:id="@+id/lockerOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#8000"
        android:visibility="gone" >

        <!--
        <TextView android:id="@+id/lockerIcon"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:gravity="center" android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" android:layout_marginBottom="70dip"
            android:text="@string/touch_lock" android:textAppearance="?android:attr/textAppearanceLarge"
            android:background="@drawable/dialpad_lock" />
        -->
    </com.csipsimple.ui.incall.locker.ScreenLocker>

       <!--   <ToggleButton
            android:id="@+id/PTT_button3"
            android:layout_width="0dp"
            android:layout_height="fill_parent"

            android:text="@string/ptt3" 
            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"
            />   -->   

</RelativeLayout>

in_call_card:

<?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/call_card_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:padding="2dip"
    tools:ignore="Overdraw" >

    <com.csipsimple.widgets.MaxScaleImageView
        android:id="@+id/contact_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/call_action_bar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/empty_description"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_contact_picture_180_holo_light" /> 

    <RelativeLayout
        android:id="@+id/contact_name_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9e808080"
        android:gravity="center_vertical" >
  <!--     
            <EditText
        android:id="@+id/editText1"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:ems="10" >



        <requestFocus />
        </EditText>
       --> 

        <com.csipsimple.widgets.MarqueeTextView
            android:id="@+id/contact_name_display_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="5dip"
            android:singleLine="true"
            android:textColor="@android:color/white"
            android:textSize="25sp" />

        <com.csipsimple.widgets.MarqueeTextView
            android:id="@+id/contact_name_sip_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/contact_name_display_name"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="2dip"
            android:singleLine="true"
            android:textColor="@android:color/white"
            android:textSize="15sp" />

        <Chronometer
            android:id="@+id/elapsedTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:textSize="20sp" />


    </RelativeLayout>

    <TextView
        android:id="@+id/call_status_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/contact_name_bar"
        android:layout_marginTop="1dip"
        android:background="#c818617b"
        android:gravity="center_vertical|right"
        android:padding="3dip"
        android:text="@string/call_state_calling"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="15sp" />

    <RelativeLayout
        android:id="@+id/call_secure_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/call_status_text"
        android:layout_marginTop="1dip"
        android:background="#c8d4aa00" >


        <ImageView
            android:id="@+id/call_secure_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_margin="2dip"
            android:contentDescription="@string/empty_description"
            android:src="@drawable/stat_sys_vp_phone_call" />

        <TextView
            android:id="@+id/call_secure_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/call_secure_icon"
            android:padding="2dip"
            android:textColor="@android:color/white"
            android:textSize="15sp" />
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/call_action_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/end_call_bar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_marginTop="2dip"
        android:background="#000000" />

    <LinearLayout


        android:id="@+id/end_call_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="2dip"
        android:background="@drawable/end_call_background"
        android:orientation="horizontal" >

        <!--   android:onClick="pushtotalk3" -->
         <ToggleButton
            android:id="@+id/PTT_button5"
            android:layout_width="0dp"
            android:layout_height="fill_parent"

            android:text="@string/ptt5" 
            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"
            />      

        <ImageButton
            android:id="@+id/endButton"
            style="?attr/actionButtonStyle"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="50"
            android:contentDescription="@string/done"
            android:minHeight="64dip"
            android:src="@drawable/ic_end_call" />


    </LinearLayout>


</RelativeLayout>
4

2 回答 2

0

如果您使用setContentView(int),则必须使用findViewById(int),因为该活动会为您膨胀 XML。

目前,您将侦听器放在未显示的按钮上,因为您从已膨胀的布局中获取它而不是从活动持有的布局中获取它。

2个解决方案:

  • 添加缺少的结束setContentView(otherLayout)调用以告诉您的活动显示您已膨胀的布局
  • 使用setContentView(R.layout.in_call_card)然后检索视图,仅findViewById(R.id.PTT_button5);用于告诉您活动显示布局“in_call_card”,然后要求它在其层次结构中搜索 ID 为“R.id.PTT_button5”的视图
于 2013-10-25T15:31:48.697 回答
0

这不是你的听众。您的听众可以正常工作,无论是使用ToggleButton或任何其他类型的视图。最后return false,你onTouchListener应该被调用,并且onClickListener- 如果存在 - 被调用。

按下按钮时您什么也得不到的原因是,按钮不在您的屏幕上。您在代码中对其进行充气,但没有父级,我看不到任何代码,这实际上将充气添加ToggleButton到任何布局中。所以,无论你在屏幕上按下什么按钮,都不是你onTouchListener听的那个按钮。

我建议ToggleButton你成为你的一部分,layout或者 - 根据你的应用程序设计 - 为此创建一个新LayoutActivity

编辑根据发布的布局。

我的理解是,你有两个完整的布局:in_card_main.xmlin_call_card.xml只有一个Activity. 那正确吗?如果是,解决方案是为每个布局设置一个 Activity 并使用startActivity().

于 2013-10-25T15:31:58.563 回答