2012-08-03 90 views
2

我想把當前顯示的字符串從TextSwitcher(字符串將被置於一個的字符串數組中)與其他字符串數組,我不知道哪一種方法,我應該做的,因爲.getText不`噸工作textSwitcher。從TextSwitcher添加字符串數組

如何加載和添加字符串到現有的但不使用在這個活動字符串數組?

我在網上找到了這個方法,爲什麼它不起作用?

Resources res = getResources(); 
String[] favorites = res.getStringArray(R.array.favorites); 
String[] planets = res.getStringArray(R.array.planets_array); 
String[] temp = new String[favorites.length+1]; 
System.arraycopy(favorites,0,temp,0,favorites.length); 
temp[favorites.length] = planets[mCounter]; 
favorites = temp; 

回答

0

TextSwitcher不提供getText方法。因此,每次調用setText時,都必須記住私有字段中的當前字符串。

class MyActivity extends Activity { 
    String currentlyDisplayedText; 
    String[] yourArray = new String[5]; 

    public void someMethod() { 
     currentlyDisplayedText = "The new text"; 
     theTextSwitcher.setText(currentlyDisplayedText); 
    } 

    public void someOtherMethod() { 
     yourArray[0] = currentlyDisplayedText; 
    } 
} 
3

TextSwitcher被實現爲容器具有兩個TextViews。當您撥打TextSwitcher.setText()時,它將在未顯示的TextView上調用TextView.setText(),然後使用可選動畫在它們之間切換。

雖然TextSwitcher不公開getText()方法,可以檢索當前顯示TextView孩子,然後調用它的getText()讓顯示的文字:

TextSwitcher textSwitcher = findViewById(...); 
TextView currentlyShownTextView = (TextView) textSwitcher.getCurrentView(); 
String currentlyShownText = textSwitcher.getText().toString();