2013-08-31 7 views
0

Хорошо, у меня есть два ImageButtons, и я настроил их точно так же, но только PLAY вызывается с помощью прослушивателя onClick. Если я нажимаю на другой, он не регистрируется вообще, что он был нажат. Когда я их отлаживаю, он показывает, что мой findViewById регистрирует объект в нужном виде и все. Я все равно получаю touchhevent, но кнопка Close не появляется в моем методе OnClick. Возможно, файл xml играет роль в этой ошибке, потому что я не могу придумать ничего, что могло бы произойти.OnClick работает только для одного ImageButton

фрагмент кода

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 

    View v = inflater.inflate(R.layout.playback_frag, container, false); 

    //BUTTONS 
    close = (ImageButton) v.findViewById(R.id.x_close); 
    play = (ImageButton) v.findViewById(R.id.play1); 

    btnListener = new OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      Log.e("in btnListener", "howdy"); 
     } 
    }; 

    close.setOnClickListener(btnListener); 
    play.setOnClickListener(btnListener); 


    return v; 
} 

расположение XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="166dp" 
    android:background="#D8000000" 
    > 
    <!-- Start of title bar --> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     > 

      <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="38dp" 
      android:weightSum="1" 
      android:background="#CC33b5e5" > 

     <TextView android:id="@+id/playback_title" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:textSize="14sp" 
        android:gravity="center_vertical" 
        android:textAllCaps="true" 
        android:paddingLeft="6dp" 
        android:textColor="#FFFFFF" 
        android:layout_weight=".4" 
        /> 



       <TextView android:id="@+id/playback_sample_rate" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:textColor="#FFFFFF" 
        android:textSize="12sp" 
        android:paddingLeft="16dp" 
        android:gravity="center_vertical"   
        android:layout_weight=".2" 
        /> 

       <TextView android:id="@+id/playback_file_size" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:textColor="#FFFFFF" 
        android:textSize="12sp" 
        android:gravity="center_vertical"      
        android:paddingLeft="16dp" 
        android:layout_weight=".2" 
        /> 

        <ImageButton android:id="@+id/x_close" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingLeft="16dp" 
        android:background="#00000000" 
        android:layout_gravity="center_vertical" 
        android:src="@android:drawable/ic_menu_close_clear_cancel" 
        android:gravity="center_vertical" 
        /> 
       </LinearLayout> 
    </LinearLayout> 
    <!-- End of title bar --> 

     <!--start of details --> 
     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height = "118dp" 
      android:layout_alignParentTop="true" 
      android:paddingTop="44dp" 
      > 
      <TextView android:id="@+id/playback_details" 
       android:paddingRight="6dp" 
       android:paddingLeft="6dp" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       /> 
     </ScrollView> 


     <!--end of details --> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="bottom" 
     android:layout_marginBottom="16dp" 
     > 
      <!-- Start of slider --> 
      <LinearLayout android:id="@+id/sliderANDplay" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="32dp"> 

        <ImageButton 
         android:id="@+id/play1" 
         android:layout_marginLeft="12dp" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="#80000000" 
         android:padding="5dp" 
         android:scaleX=".80" 
         android:scaleY=".80" 
         android:src="@drawable/av_play" /> 

        <SeekBar 
          android:id="@+id/slider1" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:max="60" 
          android:progress="0" 
          android:secondaryProgress="0" /> 
      </LinearLayout> 
      <!-- End of slider --> 

      <!-- Start of times --> 
      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="32dp" 
       android:gravity="right|bottom" 
       > 


         <TextView 
          android:id="@+id/curr_pos1" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:paddingRight="1dp" 
          android:text="@string/init_time_curr" 
          android:textColor="#ffffff" 
          android:textSize="12sp" /> 
         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="/" 
          android:textColor="#ffffff" 
          android:paddingRight="1dp" 
          android:textSize="11sp" /> 

         <TextView 
          android:id="@+id/max_duration1" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginRight="17dp" 
          android:text="@string/init_time_max" 
          android:textColor="#ffffff" 
          android:textSize="12sp" /> 

       </LinearLayout> 
      <!-- End of times --> 


    </RelativeLayout> 

ответ

0

Вам нужно либо создать два отдельных onClickListeners (по одному для каждой кнопки) или проверить, какая кнопка была нажата в одном который вы используете.

btnListener = new OnClickListener(){ 
    @Override 
    public void onClick(View v){ 

     if (v.getID == R.id.x_close) 
      Log.e("in btnListener", "howdy"); 
     else if (v.getID == R.id. play1) 
     // do something else 
    } 
}; 
+0

Право, это то, что я планирую делать, но когда я нажимаю кнопку закрытия, он даже не регистрируется в прослушивателе onClick, на который он был нажат. –

+0

странный. Вы тогда пытались к индивидуальным слушателям? – pumpkee

+0

Если по отдельному слушателю вы имеете в виду «реализует OnClickListener» на фрагменте, то да (и затем выполняете 'setOnClickListener (this);') –