android 如何设置onclick图像视图在其他图像视图上

rjee0c15  于 2022-12-02  发布在  Android
关注(0)|答案(2)|浏览(148)

我有一个xml布局,当我在java代码中将onClick设置为id为iv_contact的imageview和id为iv_user的imageview时,顶部的imageview不工作,它捕获了底部ImageView的onClick事件,如何解决这个问题?
这是我的xml布局:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/cl_contains_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv_contact"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:scaleType="centerCrop" />

    <androidx.cardview.widget.CardView
        android:id="@+id/cv_video_send"
        android:layout_width="@dimen/dp0"
        android:layout_height="@dimen/dp0"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="@dimen/dp12"
        android:layout_marginRight="@dimen/dp12"
        app:cardCornerRadius="@dimen/dp16"
        app:layout_constraintHeight_percent="0.25"
        app:layout_constraintWidth_percent="0.3"
        app:layout_constraintWidth_min="@dimen/dp110"
        app:layout_constraintHeight_min="@dimen/dp175">

        <ImageView
            android:id="@+id/iv_user"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

下面是setOnClick代码:

ivImageUser.setOnClickListener(v -> {
    Intent getIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(getIntent, REQUEST_USER_IMAGE);
});
ivContact.setOnClickListener(v -> {
    Intent getIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(getIntent, REQUEST_CONTACT_IMAGE);
});

我尝试了不同的方法,如设置android:clickable="true",但结果不是很积极,任何帮助都非常感谢。谢谢大家

ukqbszuj

ukqbszuj1#

您好,请使用iv_contact Imageview CardView的底部,它将工作。

<androidx.cardview.widget.CardView
    android:id="@+id/cv_video_send"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="12dp"
    android:layout_marginRight="@dimen/margin_12dp"
    app:cardCornerRadius="@dimen/text_size_16"
    app:layout_constraintHeight_min="175dp"
    app:layout_constraintHeight_percent="0.25"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintWidth_min="@dimen/margin_110dp"
    app:layout_constraintWidth_percent="0.3">

    <ImageView
        android:id="@+id/iv_user"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        

</androidx.cardview.widget.CardView>

<ImageView
    android:id="@+id/iv_contact"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
xmq68pz9

xmq68pz92#

确保请求代码REQUEST_USER_IMAGEREQUEST_CONTACT_IMAGE具有不同的值,例如REQUEST_USER_IMAGE = 100REQUEST_CONTACT_IMAGE = 200,并在onActivityResult()中进行相应处理

if(requestCode == 100)
    doSomeOperations();
else if(requestCode == 200)
    doSomeMoreOperations();

注意事项:
不要使用过时的方法'startActivityForResult()'。不推荐使用。
试试这个办法:

public void openSomeActivityForResult() {
    Intent intent = new Intent(this, SomeActivity.class);
    someActivityResultLauncher.launch(intent);
}

// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // There are no request codes
                    Intent data = result.getData();
                    doSomeOperations();
                }
            }
        });

相关问题