문제

How can I detect whether or not an Image View has a picture in it upon a button click. I had a class that displays a picture and some results, but I don't want the user to be able to access those results (pushing the button) until the results have been written to that class.

So, I figured I needed to check to see if there was a picture displayed in the ImageView yet.

So something like:

        case R.id.previous_box:
            if (R.id.photoResultView == null){
                            //throw up a dialog box saying they need to snap a pic first.

            }


            Intent j = new Intent(DTF.this, Result.class);
            startActivity(j);
            return;

but obviouslt R.id.photoResultView == null isn't the right way to do it...anyone know how?

Tanks!

EDIT: Line 184

    if (photoResultView.getDrawable() == null) {

EDIT:

        case R.id.previous_box:

            if (photoResultView.getDrawable() == null) {
                showToast(DTF.this, "You have to take a picture first");
                return;
            }
            Intent j = new Intent(main.this, Result.class);
            startActivity(j);
            return;
도움이 되었습니까?

해결책

Try, instead of the if block you currently have, this:

ImageView photoResultView = (ImageView)findViewById(R.id.photoResultVew);
if (photoResultView.getDrawable() == null) {
    //throw dialog
}

ImageView.getDrawable() returns either a Drawable or null (if there is no Drawable set).

Basically, say you have something like this:

public class HelloAndroid extends Activity {   
    ImageView photoResultView; //this creates a class variable
    /** Called when the activity is first created. */   
    @Override   
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);       
        setContentView(R.layout.main);
        //this assigns it to the photoResultView you defined in your XML
        photoResultView = (ImageView)findViewById(R.id.photoResultView);
    }
}

다른 팁

public class MyActivity extends Activity {

 private ImageView imageView;

 protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_records);
   imageView = (ImageView) findViewById(R.id.image_view);

 }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top