偶然間遇到的一個(gè)問題扳肛,自定義view上傳圖片進(jìn)度條隅忿,發(fā)現(xiàn)下層的控件還可以選迫靖。懊蒸。拇泛。后來改成自定義dialog了
抽空找了找資料 http://wenda.so.com/q/1447517100721093 發(fā)現(xiàn)好像沒什么卵用滨巴。。俺叭。
下面來干貨:
方法:對(duì)當(dāng)前LinearLayout(id : layout)增加監(jiān)聽就行了
findViewById(R.id.layout).setOnClickListener(null);
布局如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bxlt.demo01.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Activity:
findViewById(R.id.img).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "onClick: img");
}
});
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "onClick: btn");
}
});
此刻點(diǎn)擊圖片或者Button都是可以響應(yīng)的恭取,怎么才能只響應(yīng)上層的Button,而不響應(yīng)下層的圖片呢熄守?
加上這句:
findViewById(R.id.layout).setOnClickListener(null);
完美解決