提问人:Vasant Raval 提问时间:8/2/2023 更新时间:8/2/2023 访问量:11
根据特定条件启用和禁用视图
Enabling and Disabling a View Based on Specific Conditions
问:
我实现了一个下载按钮(ImageView),该按钮在单击时执行一些验证过程。如果所有验证都成功通过,则应用将导航到各自的活动。现在,我的要求是在单击下载按钮时禁用它,并且我想在链接验证期间的特定时间点再次启用它。
我需要在每个“if”或“else”条件中启用该按钮,其中不会发生导航到新意图。我在代码中添加了注释,以指示应在何处启用该按钮。
此外,在成功完成链接验证过程并且应用导航到新活动后,我还希望在用户从该活动返回时重新启用该按钮。
我尝试使用以下代码实现此功能,但它没有按预期工作:
private boolean isButtonEnabled = true;
//Btn is in OnCreate
downloadBtnCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isButtonEnabled) {
editTextValue[0] = Objects.requireNonNull(editText.getText()).toString();
// Step 1: Check if the network is available
if (isNetworkAvailable(getApplicationContext())) {
// Step 2: Check the network speed
try {
linkValidationAndUrl(editTextValue[0]);
downloadBtnCard.setClickable(false);
isButtonEnabled = false;
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Show a message to the user indicating no network connection
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(R.string.noInternet);
Toast.makeText(getApplicationContext(), "No Internet", Toast.LENGTH_SHORT).show();
}
}
}
});
public void linkValidationAndUrl(String url) throws IOException {
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.white));
textStatus.setText(getString(R.string.validating_link));
if (url.startsWith("https://www.threads.net") || url.startsWith("https://threads.net")) {
MainNav(url, new AccountStatusCallback() {
@Override
public void onAccountStatusReceived(String[] accountStatus) {
// Regex pattern for profile link
Pattern profileLinkPattern = Pattern.compile("^https://(?:www\\.)?threads\\.net/@[^/]+$");
Pattern post1Pattern = Pattern.compile("(?<=.net\\/t\\/).*(?=\\/\\?)|(?<=post\\/).*");
Matcher profileMatcher = profileLinkPattern.matcher(url);
Matcher post1Matcher = post1Pattern.matcher(url);
if (accountStatus[0].equals("true")) {
textStatus.setText(null);
Toast.makeText(getApplicationContext(), "Broken Url or Private Profile", Toast.LENGTH_SHORT).show();
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.brokenorprivateStr));
//Enable the BTN
downloadBtnCard.setClickable(true);
isButtonEnabled = true;
} else if (accountStatus[0].equals("unexpected")) {
textStatus.setText(null);
Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.unexpected_error));
//Enable the BTN
downloadBtnCard.setClickable(true);
isButtonEnabled = true;
} else if (accountStatus[0].equals("false")) {
Toast.makeText(getApplicationContext(), "Page Avail", Toast.LENGTH_SHORT).show();
if (profileMatcher.matches()) {
textStatus.setText(null);
textStatus.setText(getString(R.string.profile_link));
Intent iPr = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(iPr);
} else if (post1Matcher.find()) {
textStatus.setText(null);
if (accountStatus[1].equals("true")) {
//Navigate to comment page
textStatus.setText("Comment Link Provided");
Intent iC = new Intent(getApplicationContext(), CommentActivity.class);
startActivity(iC);
} else if (accountStatus[1].equals("false")) {
// Navigate to post page
textStatus.setText("Post Link Provided");
Intent iP = new Intent(getApplicationContext(), PostActivity.class);
startActivity(iP);
} else if (accountStatus[1].equals("unexpected")) {
textStatus.setText(null);
Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.unexpected_error));
}
} else {
textStatus.setText(null);
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.threadlink_invaliddataurl));
//Enable the BTN
downloadBtnCard.setClickable(true);
isButtonEnabled = true;
}
} else {
textStatus.setText(null);
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText("Unable To Get Data");
//Enable the BTN
downloadBtnCard.setClickable(true);
isButtonEnabled = true;
}
}
});
} else if ((!url.startsWith("https://")) && (url.contains("www.threads.net") || url.contains("threads.net"))) {
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.threadLink_NoHttps));
//Enable the BTN
downloadBtnCard.setClickable(true);
isButtonEnabled = true;
} else {
Toast.makeText(MainActivity.this, "Invailed Url", Toast.LENGTH_SHORT).show();
textStatus.setText(null);
textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
textStatus.setText(getString(R.string.threadLink_False));
//Enable the BTN
downloadBtnCard.setClickable(true);
isButtonEnabled = true;
}
}
@Override
protected void onResume() {
super.onResume();
if (isButtonEnabled) {
// If the button was clicked and the activity is resumed, enable the button again
downloadBtnCard.setClickable(true);
isButtonEnabled = true; // Reset the flag
}
}
答: 暂无答案
评论