ImageButton事件侦听器在ScrollView中不起作用 - > Horizo​​ntalScrollView

时间:2011-10-25 06:56:29

标签: android

ImageButton事件监听器无效。

这是我的代码(已更新):

XML:

<ScrollView ...>
    <LinearLayout
       android:orientation="vertical"
       ...
       >
       <HorizontalScrollView
          ...
          >
          <LinearLayout 
         android:orientation="horizontal"
     ...
         >
             <ImageButton
                 android:id="@+id/img_btn1"
     />
          </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</ScrollView>

Java代码:

public class Main extends Activity{
    ImageButton imgBtn1;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imgBtn1 = (ImageButton) findViewById(R.id.img_btn1);
        imgBtn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.w("onClick", "ImageButton Clicked");
            }
        });
    }
}

此代码无效。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

试试这个:-----

  ImageButton imgBtn1 = (ImageButton)findViewById(R.id.img_btn1);        
  imgButn1.setOnClickListener( new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "+", Toast.LENGTH_SHORT).show();
    }
   });

答案 1 :(得分:0)

这在我的方面运作良好:

  

的setContentView(R.layout.vhscroll);

     

ImageButton imageButton =(ImageButton)findViewById(R.id.imagebutton1);

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(VHScrollViewActivity.this, "You clicked image button", Toast.LENGTH_LONG).show();
        }
    });

和xml代码是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView  android:id="@+id/scrollview" android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
            <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
                    <ImageButton android:id="@+id/imagebutton1" android:layout_width="100dip" android:layout_height="100dip" android:src="@drawable/sample_0" android:scaleType="fitXY"/>
</.......>