在android中点击按钮后显示图像

时间:2014-10-10 08:14:22

标签: android

目前我正在尝试在运行时显示图像,但每当android应用程序正在读取时,都会通过读取activity_main.xml文件自动显示图像。

我正在尝试在同一个活动页面中显示我的结果图片。

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

    }

在上面的代码中,在单击提交按钮之前显示图像。 单击提交按钮后,请帮助我在同一活动页面中显示图像。

2 个答案:

答案 0 :(得分:1)

android:src="@drawable/friendship"

中删除activity_main.xml

答案 1 :(得分:0)

您需要设置 ImageView 的可见性,以便首先显示either from java or from xml,然后在按钮操作内单击侦听器,您可以更改其可见性。

来自XML

android:visibility="invisible"

这将使ImageView不可见。 (您也可以使用消失而不是隐身)。

在按钮的动作侦听器中使用以下内容:

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