如何隐藏ImageView并在5秒后显示它

时间:2014-05-23 20:44:10

标签: android

我想要隐藏图像,然后在10-20秒后使其可见。

这是我的代码:

public class MainActivity extends Activity  implements OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView imgv1 = (ImageView) findViewById(R.id.cool);
    imgv1.setOnClickListener(this);

    // This is the image which I want to first hide and then show it after few seconds

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
public void onClick(View v) {
    Intent startactivity = new Intent(this,signin.class);
    startActivity(startactivity);
}
}

2 个答案:

答案 0 :(得分:3)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView imgv1 = (ImageView) findViewById(R.id.cool);

    imgv1.setVisibility(View.INVISIBLE);
        new Handler().postDelayed(new Runnable(){
            public void run() {
                imgv1.setVisibility(View.VISIBLE);
            }
        }, timeInMillis);

    imgv1.setOnClickListener(this);
}

timeInMillis是您要等到ImageView之前要等待的时间。

答案 1 :(得分:0)

onCreate()

中的

imgv1.setVisibility(View.GONE);
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        imgv1.setVisibility(View.VISIBLE);
    }
},10 * 1000); // For 10 seconds