[英]making submit button clickable and not clickable
我試圖讓我的應用程序提交按鈕在用戶拍照之前不可單擊。 因此最初該按鈕將不可單擊,然后當該人拍照時,提交按鈕將變為可單擊狀態,以便他們繼續前進。 問題是我無法使其正常工作。 截至目前,提交按鈕在首次加載時不可點擊(這是我想要的)。 但是,如果我按了相機按鈕,然后按了后退按鈕,則提交按鈕變為可見(我希望它不可單擊)。 我該如何糾正它,以便在這種情況下不顯示“提交”按鈕? 到目前為止,我嘗試在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);
}
}
移動這條線
btnSubmit.setEnabled(true);
在此行之后轉到onActivityResult()方法
imageView.setImageBitmap(photo);
您的代碼看起來不錯,但我認為,如果您僅使用按鈕的可見性,它將看起來更好:
btnSubmit.setVisibility(View.GONE);
btnSubmit.setVisibility(View.VISIBLE);
並且您必須添加:
btnSubmit.setEnabled(true);
到您的onActivityResult()
方法中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.