使按钮不可见 5 秒
Make button invisible for 5 seconds
目标:
当您启动应用程序时,按钮应该在 5 秒后可见。
In order words, Start the app -> Hide a button -> Wait 5 seconds -> Display the button
问题:
代码无效,我缺少哪一部分?
信息:
*我是 android 的新手,这是我尝试的
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();
}
}
任何帮助将不胜感激谢谢
xml android清单
<?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>
好像你错过了setContentView
..先解决这个问题..
setContentView(R.layout.main_activity);
不要使用 Thread
sleep
。使用 handler post delayed
代替..
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);
无需动作动画即可轻松制作动画。设置duration即可,动画结束后监听器会显示
buttonvVew.setVisibility(View.GONE);
buttonView.animate()
.setDuration(5000)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
buttonView.setVisibility(View.visible);
}
});
试试这个
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 秒后可见。
In order words, Start the app -> Hide a button -> Wait 5 seconds -> Display the button
问题:
代码无效,我缺少哪一部分?
信息:
*我是 android 的新手,这是我尝试的
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();
}
}
任何帮助将不胜感激谢谢
xml android清单
<?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>
好像你错过了setContentView
..先解决这个问题..
setContentView(R.layout.main_activity);
不要使用 Thread
sleep
。使用 handler post delayed
代替..
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);
无需动作动画即可轻松制作动画。设置duration即可,动画结束后监听器会显示
buttonvVew.setVisibility(View.GONE);
buttonView.animate()
.setDuration(5000)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
buttonView.setVisibility(View.visible);
}
});
试试这个
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);
}
}