如何显示几秒钟的textview然后使其隐藏?

时间:2013-06-21 19:30:58

标签: android

这个网站是最好的,它帮了我很多...我是创建android applecation的初学者。 这是我第一次在这里问一个问题..我的问题是如何显示一个textview只是5秒钟让它消失..当我搜索时我发现了一些代码,但我不知道如何使用它或者我使用它以错误的方式..所以任何人都可以给我一个非常简单的例子,如何做到这一点? 我真的很感激你的帮助......>>(我不希望文字消失,我希望洞文本视图消失)

6 个答案:

答案 0 :(得分: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();

或者,更好的是,只使用动画:

已编辑(感谢@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);

答案 1 :(得分:5)

如果您希望计时器显示

,一种方法是使用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     
    }   
}

您还可以使用TimerTask

Here is a SO answerTimerTask

为例

还有其他各种方式。您可以搜索文档或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);

答案 2 :(得分:1)

        final TextView textView = new TextView(this);
        // add textView on some Layout
        textView.setText("Text or smth. from resource");        
        CountDownTimer timer = new CountDownTimer(5000, 1000) {

                        @Override
                        public void onTick(long millisUntilFinished) {
                        }

                        @Override
                        public void onFinish() {
                            textView .setVisibility(View.INVISIBLE); //(or GONE)
                            }
                    }.start();

答案 3 :(得分:1)

您可以使用HandlerRunnable来执行此操作。

public class MainActivity extends Activity {
private Handler h;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt=(TextView)findViewById(R.id.txt);
    Runnable r=new Runnable(){

        @Override 
        public void run() {
            // TODO Auto-generated method stub
            Log.e("bf", "fd");
            h.postDelayed(this, 500);
            if (txt.isShown()){
                txt.setVisibility(View.INVISIBLE);

            }
            else{
                txt.setVisibility(View.VISIBLE);
            }
        }

    };
    h=new Handler();
    h.post(r);
}
 }

答案 4 :(得分:1)

您可以将Handler与runnable一起使用。 就像我正在根据自己的需要做事。

我有一个Activity1,从那里我要在更新配置文件后更新到Activity2,我必须在Activity1中显示“成功更新”文本,因此我在AppConstants中创建了一个标志“ PERSONAL_DETAIL_UPDATE”并将其设置为“ true” “”,以在Activity2中成功更新个人资料并从那里调用Activity1以向用户显示更新的个人资料。

现在在Activity2中使用此代码向我显示“成功更新”文本3秒钟,并在3秒钟后使其消失。 我正在使XML中的TextView的可见性默认消失,因为您可以根据需要修改此代码。

enter code here



 if (AppConstants.PERSONAL_DETAIL_UPDATE) {
        personalDetailUpdateSuccessTV.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                personalDetailUpdateSuccessTV.setVisibility(View.GONE);
            }
        }, 3000);
    }

答案 5 :(得分:1)

您可以为此使用 CounDownTimer

if (password.text.toString() == preferences.getPassword()) {
            textView.text = ""
            endLockScreen.finishLockScreen()

        } else {
            textView.text = "Wrong Password"
            val timer = object : CountDownTimer(1000, 1000) {
                override fun onTick(millisUntilFinished: Long) {}
                override fun onFinish() {
                    textView.text = ""
                }
            }.start()
        }
相关问题