我想在 TextView 中显示数字,如下所示:
Wait 5 sec // then a delay of 1 sec
Wait 4 sec // display this in the same text view along with delay
Wait 3 sec // display this in the same text view along with delay
Wait 2 sec // display this in the same text view along with delay
Wait 1 sec // display this in the same text view along with delay
我想在单击按钮时执行此操作,它应该像倒计时器一样工作。
非常简单,您需要使用倒计时器 http://developer.android.com/reference/android/os/CountDownTimer.html class.
这是我刚刚为您创建的一个工作演示。
MainActivity.java
public class MainActivity extends Activity
{
int remaningTime = 5;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById( R.id.txtTextView );
Button button1 = (Button) findViewById( R.id.button1 );
button1.setOnClickListener( new OnClickListener ()
{
@Override
public void onClick(View arg0)
{
new CountDownTimer ( remaningTime * 1000, 1000 )
{
@Override
public void onFinish()
{
}
@Override
public void onTick(long millisUntilFinished)
{
textView.setText( "Wait " + ( remaningTime-- ) + " sec" );
}
}.start();
}
});
}
}
主文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtTextView"
android:layout_below="@+id/txtTextView"
android:layout_marginTop="34dp"
android:text="Button" />
</RelativeLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)