如何让我的按钮隐藏3秒?

时间:2017-10-15 15:07:13

标签: java android

我已查看此链接:1- How to show a button after 5 seconds in android studio?

2- How make a button invisible for 1 or 2 second on another button click

但是我无法理解我的代码中的这个处理程序应该在哪里。我试过但没有奏效。 “在你回家的时候听到一声尖叫”,在这篇文章之后我想让我的按钮可见。我希望我能表达自己。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    final TypeWriter tw = (TypeWriter) findViewById(R.id.tv);

            tw.setText("");
            tw.SetCharacterDelay(120);
            tw.animatedText("While u r going to home heard a scream ")






    Button btn2 = (Button)   findViewById(R.id.button3);
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent c = new Intent(Main2Activity.this,Main3Activity.class);
            startActivity(c);
        }
    });



}


}

2 个答案:

答案 0 :(得分:1)

试试这个。

Button btn2 = (Button)   findViewById(R.id.button3);

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        //Do something after 3 secs
    }
}, 3000);

答案 1 :(得分:0)

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

    Button b = (Button) findViewById(R.id.btn);


    invisibleButton(b);

}

private void invisibleButton(final View view){

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            view.setVisibility(View.GONE);
        }
    }, 1000 * 3);

  }
}

但请记住,您的import Handler 应该是 - :

import android.os.Handler;

修改

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

    Button b = (Button) findViewById(R.id.button5);
    b.setVisibility(View.GONE);

    visibleButton(b);



}

private void visibleButton(final View view){

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            view.setVisibility(View.VISIBLE);
        }
    }, 1000 * 3);

  }
}