我有一個AlertDialog顯示,並希望文本倒計時。在嘗試按以下方式實現它時,對話框完全按照需要顯示,但我試圖弄清楚如何在顯示對話框時將其中一個TextView更改爲定時器。當我嘗試以這種方式實現它時,它會崩潰,就像你在日誌信息中看到的那樣。如何在AlertDialog顯示時動態更改文本?
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)
你是什麼意思,「因爲我不能改變文本」?編譯錯誤?應用崩潰?應用掛起?開發OS崩潰? –