Android OnClick仅适用于一个ImageButton
好的,我有两个ImageButton,我对它们进行了完全相同的设置,但是onClick侦听器只调用PLAY。如果我点击另一个,它根本不会注册它被点击。当我调试它们时,它显示我的findViewById正在将对象注册到正确的视图和所有内容。我仍然会得到一个touchevent,但是关闭按钮没有出现在我的OnClick方法中。也许xml文件在这个bug中起了作用,因为我想不出还会发生什么 片段代码Android OnClick仅适用于一个ImageButton,android,onclick,Android,Onclick,好的,我有两个ImageButton,我对它们进行了完全相同的设置,但是onClick侦听器只调用PLAY。如果我点击另一个,它根本不会注册它被点击。当我调试它们时,它显示我的findViewById正在将对象注册到正确的视图和所有内容。我仍然会得到一个touchevent,但是关闭按钮没有出现在我的OnClick方法中。也许xml文件在这个bug中起了作用,因为我想不出还会发生什么 片段代码 public View onCreateView(LayoutInflater inflater, V
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>
您需要创建两个单独的onClick监听器(每个按钮一个),或者检查您正在使用的一个监听器中按下了哪个按钮
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侦听器中注册它已被单击。如果单个侦听器的意思是
在片段上实现OnClickListener
,那么是(然后执行setOnClickListener(this);
)否,我的意思是使用两个不同的侦听器。每个按钮一个。play.new OnClickListener(){@Override public void onClick(View v){Log.e(“在btnListener中”,“你好”);};和close.new OnClickListener(){@Override public void onClick(View v){Log.e(“在btnListener中”,“howdy close”);};我也试过了。无论如何,这不重要。我一定是在找别的东西。这是一个碎片,所以我不知道这是否重要?