设置 X 和 Y 坐标时按钮单击事件不起作用

Button click event not working when set X and Y coordinates

我已经创建了简单的图像视图并设置了一个框架,在设置 x 和 y 坐标之前设置了按钮的 x 和 y 坐标,这次按钮点击事件有效,但是设置 x 和 y 坐标不起作用按钮点击事件。

我的Class

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private ImageView viewImage;
private Button btnCapture;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
private Uri mCapturedImageURI;

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnCapture = (Button) findViewById(R.id.btnCapture);
    btnCapture.setX(850);
    btnCapture.setY(300);
    btnCapture.setOnClickListener(this);
    viewImage = (ImageView) findViewById(R.id.viewImage);
    viewImage.setOnTouchListener(new Touch(getApplicationContext()));
}


@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void activeTakePhoto() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        String fileName = "temp.jpg";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);
        mCapturedImageURI = getContentResolver()
                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        values);
        takePictureIntent
                .putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

private void activeGallery() {
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, RESULT_LOAD_IMAGE);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case RESULT_LOAD_IMAGE:
            if (requestCode == RESULT_LOAD_IMAGE &&
                    resultCode == RESULT_OK && null != data) {

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver()
                        .query(selectedImage, filePathColumn, null, null,
                                null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
                viewImage.setImageBitmap(bitmap);

            }
        case REQUEST_IMAGE_CAPTURE:
            if (requestCode == REQUEST_IMAGE_CAPTURE &&
                    resultCode == RESULT_OK) {
                String[] projection = {MediaStore.Images.Media.DATA};
                Cursor cursor =
                        getContentResolver().query(mCapturedImageURI, projection, null,
                                null, null);
                int column_index_data = cursor.getColumnIndexOrThrow(
                        MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String picturePath = cursor.getString(column_index_data);
                cursor.close();
                Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
                viewImage.setImageBitmap(bitmap);
            }
    }
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btnCapture:
            final Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.custom_dialog_box);
            dialog.setTitle("Select Image");
            Button dialogButton = (Button) dialog.findViewById(R.id.btnExit);
            dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            Button btnChoosePath = (Button) dialog.findViewById(R.id.btnChoosePath);
            btnChoosePath.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onClick(View v) {
                    activeGallery();
                    dialog.dismiss();
                }
            });
            Button btnTakePhoto = (Button) dialog.findViewById(R.id.btnTakePhoto);
            btnTakePhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activeTakePhoto();
                    dialog.dismiss();
                }
            });
            dialog.show();
            break;
    }

}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<Button
    android:id="@+id/btnCapture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/camera_icon" />

<RelativeLayout
    android:id="@+id/FrameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ImageView
            android:id="@+id/viewImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="matrix" />
    </FrameLayout>

    <ImageView
        android:id="@+id/abc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/aa" />
</RelativeLayout>

能否将 onClick(View view) 方法替换为以下方法。

 @Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btnCapture:

                    final Dialog dialog = new Dialog(MainActivity.this);
                    dialog.setContentView(R.layout.custom_dialog_box);
                    dialog.setTitle("Select Image");
                    Button dialogButton = (Button) dialog.findViewById(R.id.btnExit);
                    dialogButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    Button btnChoosePath = (Button) dialog.findViewById(R.id.btnChoosePath);
                    btnChoosePath.setOnClickListener(new View.OnClickListener() {
                        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                        @Override
                        public void onClick(View v) {
                            activeGallery();
                            dialog.dismiss();
                        }
                    });
                    Button btnTakePhoto = (Button) dialog.findViewById(R.id.btnTakePhoto);
                    btnTakePhoto.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            activeTakePhoto();
                            dialog.dismiss();
                        }
                    });
                    dialog.show();

            break;
    }

}

已编辑答案。 改变你下面的东西MainActivity.class

btnCapture.setX(300);
    btnCapture.setY(850);

并用以下内容替换您的 xml 文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<RelativeLayout
    android:id="@+id/FrameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ImageView
            android:id="@+id/viewImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="matrix" />
    </FrameLayout>

    <ImageView
        android:id="@+id/abc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/blue_squre" />
</RelativeLayout>

<ImageButton
    android:id="@+id/btnCapture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

在你的 xml 中有一件事你使用 btnCapture id 作为 Button。然后在 .class 文件中创建 ImageButton 的对象。我不知道它是如何为你工作的。但我改了试试。