开发者

Calling an id in an xml file in your class

开发者 https://www.devze.com 2023-01-26 03:42 出处:网络
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 resu

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);

 }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消