我有一个显示的 AlertDialog,并希望文本有倒计时。在尝试以下列方式实现它时,对话框完全根据需要显示,但我试图弄清楚如何在显示对话框时使其中一个 TextViews 更改为计时器。当我尝试以这种方式实现它时,它会崩溃,正如您在日志信息中看到的那样。
public void startOverlay()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
// Get the layout inflater
LayoutInflater inflater = getLayoutInflater();
View boxLayout = inflater.inflate(R.layout.overlay, null);
builder.setView(boxLayout);
Typeface flatui = Typeface.createFromAsset(getAssets(), "fonts/Flat-UI-Font.ttf");
TextView t1 = (TextView)boxLayout.findViewById(R.id.dialogtext1);
final TextView t2 = (TextView)boxLayout.findViewById(R.id.dialogtext2);
t1.setTypeface(flatui);
t2.setTypeface(flatui);
final AlertDialog receivedBox = builder.create();
receivedBox.show();
CountDownTimer timer = new CountDownTimer(6000, 1000) {
public void onTick(long millisUntilFinished) {
final int seconds = (int) (millisUntilFinished / 1000);
log("Tick: " + seconds);
if (seconds == 0)
receivedBox.dismiss();
Handler mHandler = new Handler();
mHandler.post(new Runnable() {
@Override
public void run() {
// executed on the UI thread
t2.setText(seconds);
}
});
}
public void onFinish() {
receivedBox.dismiss();
log("done!");
beginFirst();
}
}.start();
}
日志信息:
06-24 12:01:18.977: E/AndroidRuntime(11682): FATAL EXCEPTION: main
06-24 12:01:18.977: E/AndroidRuntime(11682): android.content.res.Resources$NotFoundException: String resource ID #0x5
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.content.res.Resources.getText(Resources.java:242)
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.widget.TextView.setText(TextView.java:3773)
06-24 12:01:18.977: E/AndroidRuntime(11682): at com.workout.MainActivity$1$1.run(MainActivity.java:163)
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.os.Handler.handleCallback(Handler.java:615)
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.os.Looper.loop(Looper.java:137)
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.app.ActivityThread.main(ActivityThread.java:4918)
06-24 12:01:18.977: E/AndroidRuntime(11682): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 12:01:18.977: E/AndroidRuntime(11682): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 12:01:18.977: E/AndroidRuntime(11682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-24 12:01:18.977: E/AndroidRuntime(11682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-24 12:01:18.977: E/AndroidRuntime(11682): at dalvik.system.NativeStart.main(Native Method)