简体   繁体   English

Android-OnActivityResult按钮单击

[英]Android - OnActivityResult Button Click

I have a fragment that have 4 buttons that call a camera. 我有一个片段,其中包含调用相机的4个按钮。 And i need to know that button that i have clicked to put the image into that button... 而且我需要知道我点击过的将图像放入该按钮的那个按钮...

Can some one help me? 有人能帮我吗?

Here i call the image button click, check permissions and if all Ok, open the device camera... 在这里,我称图像按钮为单击,检查权限,如果一切正常,请打开设备摄像头...

img_first_veiculo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (permissionVerification.checkPermissionForCamera()) {
                if (permissionVerification.checkPermissionForExternalStorage()) {
                    openDeviceCamera();
                } else {
                    permissionVerification.requestPermissionForExternalStorage();
                }
            } else {
                permissionVerification.requestPermissionForCamera();
            }
        }
    });

OnActivityResult OnActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
        return;
    }

    switch (requestCode) {

        case CROP_FROM_CAMERA: {

            //TODO Set image here
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            img_first_veiculo.setImageBitmap(photo);

            break;
        }

        case PermissionVerification.CAMERA_PERMISSION_REQUEST_CODE: {

            Intent intent = new Intent("com.android.camera.action.CROP");

            intent.setDataAndType(mImageCaptureUri, "image/*");
            intent.putExtra("crop", "true");
            intent.putExtra("outputX", 640);
            intent.putExtra("outputY", 360);
            intent.putExtra("aspectX", 16);
            intent.putExtra("aspectY", 9);
            intent.putExtra("scale", true);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, CROP_FROM_CAMERA);

            break;

        }
    }
}

As jeffery suggested you need to maintain a member varable mButtonclicked in your activity class ,in onclick remember whether its 1,2,3 or 4 and onacitivy result you can set it . 正如jeffery所建议的那样,您需要在活动类中维护成员变量mButtonclicked,在onclick中记住可以设置它的1,2,3或4和onacitivy结果。 For a beginner this should be fine but there is a chance that your actvity may be destroyed so read about onSavedinstance and onRestoreinstance 对于初学者来说,这应该没问题,但是您的活动可能会被破坏,因此请阅读onSavedinstance和onRestoreinstance

There's no magic. 没有魔术。 You know what button was pressed when you receive the "on click" event method call. 您知道当您收到“ on click”事件方法调用时按下了什么按钮。 You need to keep track of that yourself, such as in an instance field in your Activity subclass. 您需要自己进行跟踪,例如在Activity子类的实例字段中。

Don't forget to save the button pressed in instance state such that if your activity is destroyed while the camera is open, you will not lose the data. 不要忘记保存在实例状态下按下的按钮,这样,如果在打开相机时破坏了您的活动,则不会丢失数据。

Try to to save your data in onActivityResult and update Ui from onResume method. 尝试将数据保存在onActivityResult中,并通过onResume方法更新Ui。

or 要么

activity.runOnUiThread(new Runnable() {
public void run() {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
img_first_veiculo.setImageBitmap(photo);
}
});

我认为您首先需要在java类中定义按钮,使用更简单的名称命名按钮将使其更容易实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM