简体   繁体   English

使提交按钮可点击而不可点击

[英]making submit button clickable and not clickable

im trying to have my apps submit button not clickable until the user takes a picture. 我试图让我的应用程序提交按钮在用户拍照之前不可单击。 so initially the button will be not clickable and then when the person takes a picture the submit button becomes clickable so they can move on. 因此最初该按钮将不可单击,然后当该人拍照时,提交按钮将变为可单击状态,以便他们继续前进。 problem is i cant get it to work properly. 问题是我无法使其正常工作。 as of right now the submit button is not clickable (which is what i want) when it first loads. 截至目前,提交按钮在首次加载时不可点击(这是我想要的)。 but if i hit the camera button then press the back button the submit button becomes visible(i want it to be unclickable). 但是,如果我按了相机按钮,然后按了后退按钮,则提交按钮变为可见(我希望它不可单击)。 how can i correct this so that it doesnt show the submit button during this situation? 我该如何纠正它,以便在这种情况下不显示“提交”按钮? o far i have tried to set not clickable in the oncreate method , and i set to clickable when the image file is made in the photobutton. 到目前为止,我尝试在oncreate方法中设置不可点击,并且在photobutton中制作图像文件时将其设置为可点击。 which didnt work. 这没有用。

public class TreeQuestionsActivity extends AppCompatActivity {

    Button btnSubmit;
    Button btnPhoto;
    ProgressBar progress;
    String mCurrentPhotoPath;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_questions_list);

        btnSubmit = (Button) findViewById(R.id.enter_button);
        btnPhoto = (Button) findViewById(R.id.photo_button);
        btnSubmit.setEnabled(false);
      }

    private void setupButton() {
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                selection();
                if (mCurrentPhotoPath == null) {
                    Toast.makeText(getApplicationContext(), "Please submit a picture of the tree before you move on",
                            Toast.LENGTH_LONG).show();

                } else if (f == false) {
                    showProgress(true);
                    new UploadTreeTask().execute(); //adds tree and then adds the dailyUpdate -> Goes to bird list activity
                    //new DbInsertTask().execute();
                } else {
                    showProgress(true);
                    treeID = tree.getId();
                    new UploadDailyTask().execute();
                }
            }
        });
        btnPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    //startActivityForResult(takePictureIntent, ACTIVITY_START_CAMERA);
                    // Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();

                    } catch (IOException e) {
                        // Error occurred while creating the File
                        Log.i(Constants.TAG, "IO Exception");
                        e.printStackTrace();
                    }
                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                Uri.fromFile(photoFile));
                        btnSubmit.setEnabled(true);
                        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                    }
                }
            }
        });
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "TREE_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        Log.d(Constants.TAG, mCurrentPhotoPath);
        return image;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case ACTIVITY_START_CAMERA:
                if (requestCode == ACTIVITY_START_CAMERA && resultCode == RESULT_OK & null != data) {

                    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
                    //to generate random file name
                    String fileName = "tempimg.jpg";

                    try {
                        Bitmap photo = (Bitmap) data.getExtras().get("data");
                        //captured image set in imageview
                        imageView.setImageBitmap(photo);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}

move this line 移动这条线

btnSubmit.setEnabled(true);

to onActivityResult() method after this line 在此行之后转到onActivityResult()方法

 imageView.setImageBitmap(photo);

your code looks fine but i think it would look better if you just played with the visibility of the button: 您的代码看起来不错,但我认为,如果您仅使用按钮的可见性,它将看起来更好:

btnSubmit.setVisibility(View.GONE);
btnSubmit.setVisibility(View.VISIBLE);

and you have to to add : 并且您必须添加:

btnSubmit.setEnabled(true);

into your onActivityResult() method 到您的onActivityResult()方法中

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

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