我有这个Android Studio应用程序,其上有一个带有drawable的ImageView,然后我点击它然后打开相机或图库,你选择图片然后ImageView更改为用户选择的图片。
我在创建帐户活动中使用此功能,当用户单击“创建帐户”按钮时,如果您尚未填写所有字段,它会向您发送警告,但是,在ImageView中如何检查它是否可以有一张新照片吗? 这就是我的代码:
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = editTextName.getText().toString().trim();
city = editTextCity.getText().toString().trim();
email = editTextEmail.getText().toString().trim();
password = editTextPassword.getText().toString().trim();
birthday = editTextBirthday.getText().toString().trim();
if (!email.isEmpty() && !name.isEmpty() && !password.isEmpty() && !birthday.isEmpty() && !city.isEmpty()) {
if (Here is supposed where i want to check if the imageView has changed)
{
Toast.makeText(CreateAccountActivity.this, "You havent choosed a image yet",
Toast.LENGTH_LONG).show();
}
Toast.makeText(CreateAccountActivity.this, "You filled up all the fields",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(CreateAccountActivity.this, "there are some fields unfilled",
Toast.LENGTH_LONG).show();
}
}
});
答案 0 :(得分:3)
我认为你真的不想。您想要做的是检查用户是否选择了新图片。在您的活动中有一个布尔值pictureSelected。启动时将其设置为false。在imageView中更改drawable时,将其设置为true。然后在这里查看if语句中的pictureSelected。
答案 1 :(得分:2)
在用户选择图片之前,请保存drawable
的当前ImageView
,并onClick
将旧图片与当前版本进行比较
在选择图像之前:
Drawable oldDrawable = imageView.getDrawable();
onClick
方法中的:
if (imageView.getDrawable() == oldDrawable)
{
Toast.makeText(CreateAccountActivity.this, "You havent choosed a image yet",
Toast.LENGTH_LONG).show();
}
您还可以使用imageView.getDrawable() == null
检查imageView是否预先没有任何附加图像