ImageButton和Button不設置點擊事件也會攔截父View的點擊事件

ImageButton和Button不設置點擊事件也會攔截父View的點擊事件


在layout中,即使不對ImageButton和Button設置點擊事件,當點擊ImageButton和Button時,也會導致父View的點擊無響應。

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    
    <LinearLayout 
        android:id="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:orientation="vertical"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:background="#EEEED1">
        <ImageButton 
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher"
            android:layout_gravity="center_horizontal"/>
    </LinearLayout>
    
      <LinearLayout 
        android:id="@+id/layout2"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:orientation="vertical"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:layout_marginTop="10dp"
        android:background="#EEEED1">
        <Button 
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Button"
            android:layout_gravity="center_horizontal"/>
    </LinearLayout>
    

</LinearLayout>


Activity:

public class MainActivity extends Activity{
	
	private LinearLayout layout1;
	private ImageButton imageButton1;
	
	private LinearLayout layout2;
	private Button button2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		layout1 = (LinearLayout) findViewById(R.id.layout1);
		imageButton1 = (ImageButton) findViewById(R.id.button1);
		layout2 = (LinearLayout) findViewById(R.id.layout2);
		button2 = (Button) findViewById(R.id.button2);
		
		//即使ImageButton和Button不設置點擊事件監聽,也會攔截事件,導致父view獲取不到點擊
		layout1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "點擊父1", Toast.LENGTH_SHORT).show();
			}
		});
		
//		imageButton1.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Toast.makeText(getApplicationContext(), "點擊ImageButton", Toast.LENGTH_SHORT).show();
//			}
//		});
		
		//即使ImageButton和Button不設置點擊事件監聽,也會攔截事件,導致父view獲取不到點擊
		layout2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "點擊父2", Toast.LENGTH_SHORT).show();
			}
		});
		
//		button2.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Toast.makeText(getApplicationContext(), "點擊Button", Toast.LENGTH_SHORT).show();
//			}
//		});
		
	}

}

不論用FrameLayout 還是RelativeLayout,點擊事件還是沒響應。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章