使按钮不可见5秒钟 [英] Make button invisible for 5 seconds

查看:117
本文介绍了使按钮不可见5秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:
启动应用程序时, 按钮应在5秒钟后可见.

Goal:
When you start the app, The Button should be visible after 5 seconds.

按顺序,启动应用程序->隐藏按钮->等待5秒钟->显示按钮

In order words, Start the app -> Hide a button -> Wait 5 seconds -> Display the button

问题:
代码不起作用,我缺少什么部分?

Problem:
The code doesn't work and what part am I missing?

信息:
*我是android的新手,这是我尝试的

Info:
*Im new in android, This what i try

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button2 = (Button) findViewById(R.id.btn_test);
        button2.setVisibility(GONE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    //dummy delay for 5 second
                    Thread.sleep(5000);
                }
                catch (InterruptedException e){
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() { //resetting the visibility of the button
                    @Override
                    public void run() {

                        //manipulating UI components from outside of the UI Thread require a call to runOnUiThread
                        button2.setVisibility(VISIBLE);
                    }
                });
            }
        }).start();
    }
}

任何帮助将不胜感激,谢谢

Any help will be appreciated thank you

xml androidmanifest

xml androidmanifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jfdimarzio.labb3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


activity_main xml


activity_main xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jfdimarzio.labb3.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:visibility="visible"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="16dp" />

</android.support.constraint.ConstraintLayout>

推荐答案

尝试一下

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main.xml);
        Button button2 = (Button) findViewById(R.id.btn_test);
        button2.setVisibility(GONE);
        button2.postDelayed(new Runnable() {
            public void run() {
                button2.setVisibility(View.VISIBLE);
            }
        }, 5000);
    }
}

这篇关于使按钮不可见5秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆