2012-08-03 43 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();