2014-01-29 55 views
0

問候!! 我想是這樣顯示錶: -如何在android中打破數組字符串文本?

  • 1 * 1 = 1
  • 1 * 2 = 2
    • -
  • 1 * 10 = 10

    但我是abl e顯示第一行然後消失,然後顯示下一行。我想用字符串數組來顯示數字。

這裏是我的代碼: -

public class MainActivity extends Activity { 
    TextView txtView; 
    static String test[]={"","1","","1 *","","1 * 1","","1 * 1 = 1", 
     "","1","","1 *","","1 * 2","","1 * 2 = 2", 
     "","1","","1 *","","1 * 3","","1 * 3 = 3", 
     "","1","","1 *","","1 * 4","","1 * 4 = 4", 
     "","1","","1 *","","1 * 5","","1 * 5 = 5", 
     "","1","","1 *","","1 * 6","","1 * 6 = 6", 
     "","1","","1 *","","1 * 7","","1 * 7 = 7", 
     "","1","","1 *","","1 * 8","","1 * 8 = 8", 
     "","1","","1 *","","1 * 9","","1 * 9 = 9", 
     "","1","","1 *","","1 * 10","","1 * 10 = 10"}; 
    Handler handler; 
    ImageView imageView; 
    Runnable runnable; 
    StringBuilder sb = new StringBuilder(); 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Intent i = getIntent(); 
     String someVariable = i.getStringExtra("MA"); 
     Toast.makeText(getApplicationContext(), "SomeVariable" +someVariable, 222).show(); 

     txtView = (TextView) findViewById(R.id.greentxt_name); 



    handler = new Handler(); 
    runnable = new Runnable() { 

     int t=0; 
     public void run() { 

      sb.append(test[t]); 

      t++; 

      if(t>=79) 
      { 


      handler.removeCallbacks(runnable) ; 
      txtView.setText(test[t]); 

      } 
      else 
      { txtView.setText(test[t]); 
      handler.postDelayed(this,1000);  
      } 
     } 
    }; 
    handler.postDelayed(runnable, -1000); //for initial delay.. 
} 


} 

任何答案是明顯的。

感謝的提前

+0

看起來你可能想要使用一個listview,而每個listviews視圖都有一個tablelayout。但是你會爲此寫一個customAdapter。 –

+0

歡迎!我想要一個一個地顯示錶格,這樣我編碼就像這樣,但我不明白如何顯示整個表格一次。 – UchihaSasuke

+0

txtView.append(test [t]); 而不是 txtView.setText(test [t]); 希望我對你有幫助。 – user2469133

回答

0

我編輯了自己的代碼位,獲得期望的結果 這裏有雲:

public class MainActivity extends Activity { 

    TextView txtView; 
    static String test[]={"1"," * ","1"," = ","1", "\n", 
     "1"," * ","2"," = ","2", "\n", 
     "1"," * ","3"," = ","3", "\n", 
     "1"," * ","4"," = ","4", "\n", 
     "1"," * ","5"," = ","5", "\n", 
     "1"," * ","6"," = ","6", "\n", 
     "1"," * ","7"," = ","7", "\n", 
     "1"," * ","8"," = ","8", "\n", 
     "1"," * ","9"," = ","9", "\n", 
     "1"," * ","10"," = ","10"}; 

    Handler handler; 
    ImageView imageView; 
    Runnable runnable; 
    StringBuilder sb = new StringBuilder(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent i = getIntent(); 
     txtView = (TextView) findViewById(R.id.greentxt_name); 



    handler = new Handler(); 
    runnable = new Runnable() { 

     int t=0; 
     public void run() { 

      sb.append(test[t]); 

      t++; 

      if(t>=59) 
      { 


      handler.removeCallbacks(runnable) ; 
      txtView.setText(sb); 

      } 
      else 
      { txtView.setText(sb); 
      handler.postDelayed(this,1000);  
      } 

     }}; 
    handler.postDelayed(runnable, -1000); //for initial delay.. 
}} 

可以刪除硬編碼值和推廣,以便它可以用於未來的任何其他表格。

+0

歡迎它的工作,但我仍然有同樣的問題。它在單行顯示,但我想在第二行後1 1 – UchihaSasuke

+0

偉大的!它爲我的工作爲我的要求。我想增加這封信的聲音,你可以幫我如何做到這一點。 – UchihaSasuke

+0

我想要一些幫助..我試着點擊按鈕的早點,但它直接查看我的第一個表可以幫助如何獲得特定的按鈕點擊不同activity.i感謝你。 – UchihaSasuke

0

,因爲從文字出現的新的索引值[T]

您可以通過連接行作爲下一個改變的邏輯有點在TextView的文本不斷得到更換在SB串線和替換

txtView.setText(test[t]); 

通過

txtView.setText(sb); 
+0

我試了兩種,但它不適合我。它顯示所有的內容。我用數字顯示數字,這就是爲什麼我使用文本[t]。所以我可以在這裏按數字顯示數字。 – UchihaSasuke

+0

利用另一個字符串跟蹤你的行(在你的情況下每次8個索引值),一旦完成,向它添加一個新行。此外,繼續添加此文本到sb –

1

你可以這樣做。 :

StringBuffer str= new StringBuffer(); 
for(int i=1;i<=10;i++) 
{ 
    str.append(""+1+" * "+i+" = "+(1*i)+"\n"); 
    Log.e("", ""+str.toString()); 
} 
txt.setText(str); 
+0

歡迎!我試着用這種方法,但我想從點擊按鈕中獲得輸入。我試過[這裏](http://stackoverflow.com/questions/21328355/how-to-set-delay-for-loop-in-android)但我無法顯示數字的數字。 – UchihaSasuke

相關問題