在类中的xml文件中调用id

时间:2010-11-17 22:01:26

标签: android xml if-statement imageview

如何在单击按钮时检测图像视图中是否有图片。我有一个显示图片和一些结果的类,但我不希望用户能够访问这些结果(按下按钮),直到结果写入该类。

所以,我想我需要检查ImageView中是否有图片显示。

类似于:

        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;

但很明显R.id.photoResultView == null不是正确的方法......任何人都知道怎么做?

坦克!

编辑:第184行

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

编辑:

        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;

2 个答案:

答案 0 :(得分:1)

尝试,而不是您目前拥有的if阻止,这个:

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

ImageView.getDrawable()返回Drawable或null(如果没有Drawable集)。

基本上,你说你有这样的事情:

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

答案 1 :(得分:0)

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

 }

}