【问题标题】:Android Studio How to check if image view is already displaying an image, if so disable intentAndroid Studio如何检查图像视图是否已经显示图像,如果是则禁用意图
【发布时间】:2019-03-09 18:40:24
【问题描述】:

我正在制作一个具有以下布局的应用程序:Main App Layout 在应用程序中,我使用“拍照”按钮从手机调用相机意图来拍照。图片被显示到图像视图中(在布局中以红色勾勒)。

我还使用保存按钮通过意图将图片保存到图库。我还使用签名按钮来捕获用户的签名。签名有自己的布局。布局包括以下内容: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();

        }
    });

【问题讨论】:

  • 你试过onActivityResult()方法吗?
  • @Boken 不,我没有!这个方法我听说过,但没用过

标签: java android android-intent imageview


【解决方案1】:

在包含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");

编辑:

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

【讨论】:

    【解决方案2】:

    你可以按照我做的下面给出的步骤来做到这一点

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

    它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多