2013-08-31 27 views
0

好吧,我有两个ImageButtons,我将它们设置完全一样,但只有PLAY才会被onClick监听器调用。如果我点击另一个,它根本没有注册它被点击。当我调试它们时,它显示我的findViewById正在将对象注册到正确的视图和一切。我仍然得到一个touchevent,但关闭按钮不会出现在我的OnClick方法中。也许xml文件在这个bug中扮演着一个角色,因为我想不出任何其他可能发生的事情。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);') –