2014-10-10 42 views
0

目前我試圖在運行時顯示圖像,但每當android應用程序正在讀取,然後通過從activity_main.xml文件讀取自動顯示圖像。在android中按鈕單擊後顯示圖像

我想在同一活動頁面中顯示我的結果圖像。

activity_main.xml中

<ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="fill_parent" 
      android:layout_height="150dp" 
      android:layout_margin="5dp" 
      android:src="@drawable/friendship" 
      android:contentDescription="@string/desc1"/> 

MainActivity.java

private void onSaveClick(){ 
     String res,fname,sname; 
     TestRelation tr = new TestRelation(); 
     Validations validate = new Validations(); 
     TextView text = (TextView) findViewById(R.id.tv); 
     EditText firstname = (EditText)findViewById(R.id.first_name); 
     EditText secondname = (EditText)findViewById(R.id.second_name); 
     fname = firstname.getText().toString(); 
     sname = secondname.getText().toString(); 
      if(validate.validate(fname, sname).equals("")){ 
       res = tr.checkRelation(fname, sname); 
       text.setText(res); 
      }else{ 
       text.setText(validate.validate(fname, sname)); 
       ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
       imageView.setImageResource(R.drawable.friendship); 
      } 
      //System.out.println("res value"+res);  

    } 

在圖像被點擊提交按鈕之前顯示上面的代碼。 點擊提交按鈕後,請幫助我在同一活動頁面中顯示圖像。

回答

0

您需要設置ImageView的可見性,以便它在開始時不顯示either from java or from xml,然後在按鈕操作內部單擊偵聽器中可以更改其可見性。

從XML

android:visibility="invisible" 

這將使ImageView的無形。 (您也可以使用不見了而不是隱形)。

而且裏面按鈕的動作偵聽器使用以下命令:

ImageView img=(ImageView)findViewById(R.id.imageview); 
img.setVisibility(View.VISIBLE); 
1

activity_main.xml

刪除 android:src="@drawable/friendship"