问题
Goal:
When you start the app, The Button should be visible after 5 seconds.
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?
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 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
<?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>
回答1:
Try this
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);
}
}
回答2:
Seems like you missing setContentView
.. fix that first..
setContentView(R.layout.main_activity);
Don't use Thread
sleep
. use handler post delayed
instead..
Handler handler = new Handler();
button2.setVisibility(View.GONE);
Runnable runnable = new Runnable() {
@Override
public void run() {
button2.setVisibility(View.VISIBLE);
}
}
// run runnable after 5 seconds
handler.postDelayed(runnable, 5000);
回答3:
So simple you animate without action animation. Just set duration, and after ending animation, the listener will display it
buttonvVew.setVisibility(View.GONE);
buttonView.animate()
.setDuration(5000)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
buttonView.setVisibility(View.visible);
}
});
来源:https://stackoverflow.com/questions/49595529/make-button-invisible-for-5-seconds