这个网站是最好的,它帮了我很多...我是创建android applecation的初学者.这是我第一次在这里问一个问题..我的问题是如何显示一个textview只是5秒钟让它消失..当我搜索时我发现了一些代码,但我不知道如何使用它或者我使用它以错误的方式..所以任何人都可以给我一个非常简单的例子,如何做到这一点?我真的很感激你的帮助...... >>(我不希望文字消失,我希望洞文本视图消失)
CSm*_*ith 12
使用AsyncTask:
new AsyncTask<Void, Void, Void>()
{
protected Void doInBackground(Void... params)
{
Thread.sleep(5000); // sleep 5 seconds
}
protected void onPostExecute (Void result)
{
// fade out view nicely
AlphaAnimation alphaAnim = new AlphaAnimation(1.0f,0.0f);
alphaAnim.setDuration(400);
alphaAnim.setAnimationListener(new AnimationListener()
{
public void onAnimationEnd(Animation animation)
{
// make invisible when animation completes, you could also remove the view from the layout
myTextView.setVisibility(View.INVISIBLE);
}
});
myTextView.startAnimation(alphaAnim);
}
}.execute();
Run Code Online (Sandbox Code Playgroud)
或者,更好的是,只使用动画:
编辑(感谢@pkk的建议):
// fade out view nicely after 5 seconds
AlphaAnimation alphaAnim = new AlphaAnimation(1.0f,0.0f);
alphaAnim.setStartOffset(5000); // start in 5 seconds
alphaAnim.setDuration(400);
alphaAnim.setAnimationListener(new AnimationListener()
{
public void onAnimationEnd(Animation animation)
{
// make invisible when animation completes, you could also remove the view from the layout
myTextView.setVisibility(View.INVISIBLE);
}
});
myTextView.setAnimation(alphaAnim);
Run Code Online (Sandbox Code Playgroud)
一种方法是使用CountDownTimer,如果你想要显示计时器
public void onCreate(...){
...
timer = new MyCountDown(5000, 1000);
}
private class MyCountDown extends CountDownTimer
{
long duration, interval;
public MyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
start();
}
@Override
public void onFinish() {
textView1.setVisibility(View.INVISIBLE);
}
@Override
public void onTick(long duration) {
// could set text for a timer here
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以使用TimerTask.
这是一个很好的例子TimerTask
还有其他各种方式.您可以搜索文档或SO来决定哪种最适合您的需求
用TimerTask示例编辑
Timer t = new Timer(false);
t.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
txt.setVisibility(View.INVISIBLE);
}
});
}
}, 5000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12325 次 |
| 最近记录: |