2010-11-17 38 views
0

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

所以,我想我需要检查是否有图片显示在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; 

但obviouslt 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; 

回答

1

尝试,而不是if块你目前有,这样的:

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

ImageView.getDrawable()收益要么被拉伸或null(如果没有可绘制集合)。

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

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

我把在,但它与非受迫性错误关闭 - 任何想法? – Sapp 2010-11-17 22:15:19

+0

你能编辑你的LogCat输出到你的文章吗? – kcoppock 2010-11-17 22:17:55

+0

photoResultView为null,它没有在视图层次结构中声明 – apps 2010-11-17 22:25:16

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

} 

} 
相关问题