【问题标题】:OnClick works for only one ImageButtonOnClick 仅适用于一个 ImageButton
【发布时间】:2013-09-01 01:32:02
【问题描述】:

好的,我有两个 ImageButton,我将它们设置为完全相同,但只有 PLAY 被 onClick 侦听器调用。如果我单击另一个,它根本不会注册它被单击。当我调试它们时,它表明我的 findViewById 正在将对象注册到正确的视图和所有内容。我仍然得到一个触摸事件,但关闭按钮没有出现在我的 OnClick 方法中。也许 xml 文件在这个 bug 中发挥了作用,因为我想不出还有什么可能发生的。

片段代码

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>

【问题讨论】:

    标签: android onclick


    【解决方案1】:

    您需要创建两个单独的 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
        }
    };
    

    【讨论】:

    • 对,这就是我打算做的事情,但是当我单击关闭按钮时,它甚至没有在 onClick 侦听器中注册它已被单击。
    • 如果单个听众的意思是片段上的implements OnClickListener,那么是的(然后做setOnClickListener(this);
    • 不,我的意思是使用两个不同的侦听器。每个按钮一个。 play.new OnClickListener(){ @Override public void onClick(View v){ Log.e("in btnListener", "howdy"); } };
    • and close.new OnClickListener(){ @Override public void onClick(View v){ Log.e("in btnListener", "howdy close"); } };
    • 我也试过了。反正应该没关系。这一定是我正在寻找的其他东西。这是在一个片段中,所以我不知道这是否重要?
    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    相关资源
    最近更新 更多