[英]ImageView not Clickable in DialogFragment
I have an ImageView for which I wanted to implement the onClickListener.我有一个 ImageView,我想为其实现 onClickListener。 But when I click on the image, nothing happens.
但是当我点击图像时,什么也没有发生。 Event the Logcat does not show any errors.
事件 Logcat 没有显示任何错误。 Something is happening with the clickability attribute.
可点击性属性发生了一些事情。
My XML code:我的 XML 代码:
<ImageView
android:id="@+id/img2"
android:layout_width="104dp"
android:layout_height="126dp"
android:layout_weight="1"
android:padding="8dp"
android:scaleType="centerCrop"
android:clickable="true"
android:src="@drawable/ic_person_black_24dp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="-3dp"
tools:layout_editor_absoluteY="14dp"
android:focusable="true" />
Code in Fragment:片段中的代码:
public class DialogFrag extends DialogFragment {
private ImageView img2;
private static final int IMAGE_PICK_CODE = 1000;
private final int CODE_MULTIPLE_IMG_GALLERY = 2;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@NonNull
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_layout, container, false);
img2 = (ImageView) view.findViewById(R.id.img2);
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, IMAGE_PICK_CODE);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int CODE_IMG_GALLERY = 1;
if (requestCode == CODE_IMG_GALLERY && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
if(imageUri!=null){
img2.setImageURI(imageUri);
}
}
if (requestCode == CODE_IMG_GALLERY && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
if(imageUri!=null){
img2.setImageURI(imageUri);
}
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
return builder
.setTitle("Заполните анкету")
.setView(R.layout.dialog_layout)
.setPositiveButton("OK", null)
.setNegativeButton("Отмена", null)
.create();
}
}
Where is my mistake?我的错误在哪里? Help please)
请帮忙)
Please override the onViewCreated() and use the view object请覆盖onViewCreated()并使用视图object
kotlin code: kotlin 代码:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
img2 = view.findViewById(R.id.img2);
// Implement onclicklistener
}
This should be of any help I guess.我猜这应该有任何帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.