Android Studio 如何检查图像视图是否已经在显示图像,如果是则禁用 Intent

Android Studio How to check if image view is already displaying an image, if so disable intent

我正在制作一个具有以下布局的应用程序:Main App Layout 在该应用程序中,我使用拍照按钮从我的 phone 中调用相机 Intent 来拍照。图片显示在图像视图中(在布局中以红色标出)。

我还使用“保存”按钮通过 intent 将图片保存到图库。我还使用签名按钮来捕获用户的 signature.Signature 有自己的布局。布局包括以下内容:Signature Layout但是,假设我打开应用程序并在当前没有显示图像时点击保存按钮。尽管没有图片,但我的保存按钮目前仍然有效并拉出画廊。我的签名布局中的保存按钮也会发生同样的事情。如果当前没有签名,保存按钮仍然保存。

我如何将其编码到可以检查当前是否已显示图片或已显示签名的位置,如果没有,我的签名和主应用程序布局中的保存按钮将被禁用。我知道禁用按钮的语法是:myButton.setEnabled(false);

我在主应用程序布局中有以下用于保存按钮的代码:

 //this save button is for the gallery app after you take a photo
 saveButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //launch the gallery app intent
            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setType("image/*");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            Toast.makeText(DriverActivity.this, "Image Saved to Gallery", Toast.LENGTH_SHORT).show();
            /*if there is currently no image, disable save button and display a toast message
            Toast.makeText(DriverActivity.this, "There's no image currently shown.", Toast.LENGTH_SHORT).show();*/

        }
    });
    // restoring storage image path from saved instance state
    // otherwise the path will be null on device rotation
    restoreFromBundle(savedInstanceState);

然后我有这个签名代码:

//this is for signature
    signatureButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            /*// Use an intent to launch an email app.
            // Send the order summary in the email body.
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("mailto:")); // only email apps should handle this
            intent.putExtra(Intent.EXTRA_SUBJECT,
                    getString(R.string.order_summary_email_subject));
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }*/
            Intent intent = new Intent(DriverActivity.this, SignatureActivity.class);
            startActivity(intent);
            Toast.makeText(DriverActivity.this, "Now Loading Signature Sign", Toast.LENGTH_LONG).show();
        }
    });

这段代码来自我的 SignatureActivity.java 文件(注意,上面的两个代码示例来自不同的 Activity.java 文件(即:DriverActivity.java):

//capture signature
    btnSave.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            view.setDrawingCacheEnabled(true);
            mSignature.save(view,StoredPath);
            Intent intent2 = new Intent(getBaseContext(), DriverActivity.class);
            startActivity(intent2);
            finish();
            Toast.makeText(getApplicationContext(), "Successfully Saved", Toast.LENGTH_SHORT).show();

        }
    });

您可以像我一样按照下面给出的步骤来完成此操作

  • 创建一个Boolean变量并将其值初始化为false
  • 当您从 Intent 获取图像时,将其值设置为 true
  • 现在您可以检查 Button click 就好像值是 false 然后做 不保存图片或数据,如果值为 true 则保存图像 或您的数据。

对我有用。

在持有ImageView的布局中,可以对照片的ImageView使用"tag"属性;如果标签是 "false" 则没有图像关联到 ImageView,然后您可以禁用保存按钮;如果是 "true" 则有一个,您可以启用保存按钮。

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="false" />

行为

ImageView image = findViewById(R.id.image);

if ((Boolean) image.getTag()) { // No image
    // Disable save button
    mBtnSave.setEnabled(false);
} else {
    mBtnSave.setEnabled(true);
}

每次拍照的时候,要把ImageView的tag改成"true"

image.setTag("true");

编辑:

当您使用不同的 activity 进行签名时,您还必须使用 startActivityForResult() 这将启动 SignatureActivity 并在完成后等待结果;您可以按照上述步骤将签名图像标签设置为 "true" 如果您收到了成功的签名,或者 "false" 如果没有。 Here 你可以找到如何使用 startActivityForResult()