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

时间:2016-02-17 08:23:56

标签: java android

我试图让我的应用提交按钮不可点击,直到用户拍照。所以最初按钮将不可点击,然后当人拍照时,提交按钮变为可点击,以便他们可以继续前进。问题是我无法让它正常工作。截至目前,提交按钮在首次加载时无法点击(这就是我想要的)。但如果我按下相机按钮然后按后退按钮,提交按钮变为可见(我希望它不可点击)。我如何纠正这一点,以便它在这种情况下不显示提交按钮?到目前为止,我试图在oncreate方法中设置不可点击,并且当在photobutton中制作图像文件时,我设置为可点击。哪个没用。

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);
    }

}

2 个答案:

答案 0 :(得分:0)

移动此行

btnSubmit.setEnabled(true);

到此行之后的onActivityResult()方法

 imageView.setImageBitmap(photo);

答案 1 :(得分:0)

你的代码看起来很好,但我认为如果你只是玩按钮的可见性会更好看:

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

你必须添加:

btnSubmit.setEnabled(true);

进入onActivityResult()方法