2014-09-27 18 views
1

我,改變着秒鐘的時間(S),分鐘,秒代碼:格式正確文本到alertdialog

int day = (int)TimeUnit.SECONDS.toDays((long) val3);   
long hours = TimeUnit.SECONDS.toHours((long) val3) - (day *24); 
long minute = TimeUnit.SECONDS.toMinutes((long) val3) - (TimeUnit.SECONDS.toHours((long) val3)* 60); 
long second = TimeUnit.SECONDS.toSeconds((long) val3) - (TimeUnit.SECONDS.toMinutes((long) val3) *60); 

也是我認爲寫代碼結果:

alertDialog.setMessage("Day\t\t" + day + "\nHour\t\t" + hours + "\nMinute\t" + minute + "\nSeconds\t" + second); 

我需要把正確的格式使用\ t從變量中分離日,小時,分鐘。問題是我不知道我有多少天/小時/分鐘。那麼,以編程方式使用\ t的最佳方式是什麼?

enter image description here

+0

這個代碼到底有什麼問題,預期的結果是什麼? – 2014-09-27 17:10:57

+0

胡安你能看到,結果不對齊 – watchmansky 2014-09-27 17:15:35

+1

哦..所以你想它對齊..diagonally..right ??? – Lal 2014-09-27 17:16:15

回答

1

enter image description here

實現類似於此的作用,創建一個自定義Dialog和使用自定義佈局了。修改內部TextView或您希望用於顯示數據的任何內容的值。在我的筆畫演示,你可以做這樣簡單的東西:

Dialog dialog = new Dialog(this); 
     dialog.setContentView(R.layout.dialog); 
((TextView)dialog.findViewById(R.id.days)).setText(days); 
((TextView)dialog.findViewById(R.id.hours)).setText(hours); 
((TextView)dialog.findViewById(R.id.minutes)).setText(minutes); 
((TextView)dialog.findViewById(R.id.seconds)).setText(seconds); 
//To show, just call dialog.show(); 

對應於R.layout.dialog佈局如下,但是請記住,您可以修改此滿足您的實際需求,改變顏色大小,佈局以及任何你想要的。我儘量保持簡潔,但我們都知道xml佈局非常龐大,即使是簡單的佈局也是如此。

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="horizontal" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Days" 
      android:id="@+id/daysLabel"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hours" 
      android:id="@+id/hoursLabel" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Minutes" 
      android:id="@+id/minutesLabel"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Seconds" 
      android:id="@+id/secondsLabel"/> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="0" 
      android:id="@+id/days" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="42" 
      android:id="@+id/hours"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="12" 
      android:id="@+id/minutes"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="59" 
      android:id="@+id/seconds"/> 
    </LinearLayout> 

</LinearLayout> 

總之,您將創建一個xml佈局文件,該文件將顯示爲對話框而不是默認的文件。這給你決定什麼地方顯示的自由,沒有限制。

1

如果您可以使用單位前的數字,那麼您可以輕鬆地將所有內容排成一行,而無需自定義對話框佈局。只需將您的setMessage更改爲:

alertDialog.setMessage(String.format("%3d Day\n%3d Hour\n%3d Minute\n%3d Seconds", 
         day, hours, minute, second));