2013-10-25 96 views
0

我有一個toggleButton,當我按下它時應該運行代碼,當我放開時,它應該會運行更多。然而,它並不像那樣工作,就像一個切換按鈕通常一樣,我按它並且它改變狀態,它根本不調用touchlistener代碼。 在我in_call_card.xml我:ontouchListener代碼沒有運行?

<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> 
+0

你怎麼知道它不會調用監聽器? – Simon

+0

Logcat?應用程序FC或給你一個錯誤?你有沒有嘗試把日誌或吐司看看是否得到這個功能? – Si8

+0

@Simon沒有記錄,沒有烤麪包等。 – Paul

回答

0

如果使用setContentView(int)您必須使用findViewById(int),因爲活動會擴展XML的y OU。

目前,您正在將聽衆放在未顯示的按鈕上,因爲您從佈局中獲得了已膨脹,但不是來自活動持有的那個。

2解決方案:

  • 添加缺少的結束setContentView(otherLayout)呼叫告訴你的活動,以顯示你已經膨脹
  • 佈局使用setContentView(R.layout.in_call_card),然後只用findViewById(R.id.PTT_button5);檢索視圖告訴你活動以在要求其搜索具有其層級中的標識符「R.id.PTT_button5」的視圖之前顯示佈局「in_call_card」的活動
+0

對我來說非常困惑。我嘗試了setContentView(otherLayout)並且顯示按鈕 - 看起來不同並且可以工作,但是現在佈局的其餘部分都被破壞了。令我困惑的是,我最初是如何展示這個按鈕的,是我是否誇大了它,但沒有附加到任何東西上?我無法弄清楚如何只添加這個按鈕並使用這個按鈕來實現主佈局。我需要這兩個xml文件中的元素才能顯示和工作。 – Paul

+0

@Paul您可以在不顯示佈局的情況下擴充佈局。當你使用'setContentView(int)'時,你告訴你的活動來擴充XML並顯示它。那麼當你使用'myactivity.findViewById(int)'的時候,它會在它所持有的膨脹視圖中進行搜索。但是當你自己膨脹一個XML資源時,如果你忘記將它設置爲你的內容視圖,你將使用**不顯示**的視圖。看來你是在這種情況下。然後你有2個解決方案(例如我的答案)。 – Flawyte

+0

看到我編輯的答案,我希望它會有所幫助。 – Flawyte

0

這不是你的聽衆。您的聽衆可以正常工作,可以使用ToggleButton或任何其他類型的視圖。 return false最後,你的onTouchListener應該被調用,並且onClickListener - 如果有的話 - 會被調用。

當你按下按鈕時你什麼也沒有得到的原因是,該按鈕不在你的屏幕上。你在代碼中誇大它,但沒有父母,我不能看到任何代碼,實際上將虛增ToggleButton添加到任何佈局。所以,無論你在屏幕上按下什麼按鈕,它都不是您的onTouchListener監聽的按鈕。

我建議讓你的ToggleButton部分layout或 - 根據您的應用程序設計 - 創造了這個新的LayoutActivity

編輯根據發佈的佈局。

我的理解是,你有兩個完整的佈局:in_card_main.xmlin_call_card.xml但只有一個Activity。是對的嗎?如果是的話,解決方案是每個佈局都有一個活動,並用startActivity()來調用活動。

+0

我真的不明白我在充氣之前如何看到一個按鈕,並在我膨脹後它不附加,我怎麼才能把它添加到當前屏幕。我的意思是,如果它顯示在屏幕上,甚至在我稱之爲通貨膨脹之前,肯定應該有辦法讓它做些事情。我認爲這是佈局的一部分,看它在屏幕上。我不認爲我必須膨脹任何東西,但是如果我沒有,我會得到一個空指針。 – Paul

+0

您在屏幕上看到的按鈕不是您在代碼中膨脹的按鈕。你的屏幕上必須有你的佈局。你可以發佈我們'in_card_main.xml'的佈局嗎(不是我們已經有的'in_call_card.xml')?基本上我認爲你的主佈局中有一個按鈕,應該是你的觸摸監聽器應該註冊的按鈕。但讓我們先看看你的主要佈局。啊,那是,謝謝。讓我們來看看...... – jboi

+0

只有一項活動是的,如果我做了另一項活動,甚至可以進入其中?我正在編輯代碼,我沒有寫這個,我沒有遵循的是他們有兩個佈局,一個活動,但他們的所有按鈕工作正常,我的沒有。當然,如果按鈕從這些佈局出現在屏幕上,有一種方法可以將它連接起來以實際工作。如果我做了另一項活動,我不確定我在做什麼。我只是讓聽衆在onCreate? – Paul