2012-10-10 193 views
2

我有一個搜索字符串,我需要用粗體樣式替換搜索字符串字符到不同的textview字符串。下面 可以編碼:字符串匹配字符用粗體不替換搜索字符串

[第一屏幕加載陣列] [1] [陣列搜索字符串] [2] [取代通過搜索字符串中的所有串的TextView] [3]

我!!不希望替換所有的textview字符串,因爲我只想從不同的textview字符串位置的粗體特定字符。

![在這裏輸入的形象描述] [4]

我想這種格式替換風格大膽。

String strlottery =「17659」;

TextView txtS = (TextView) findViewById(R.id.textView5); 
TextView txtT = (TextView)findViewById(R.id.textView6); 
TextView txt1 = (TextView) findViewById(R.id.textView9); 
TextView txt2 = (TextView) findViewById(R.id.textView13); 
TextView txt3 = (TextView) findViewById(R.id.textView17); 

String str=strlottery.replaceAll("1", "<font size=\"16\" color=\"red\"><b>"+"1"+"</b><</font>");      
Log.v("Search", str); 
Spanned text = Html.fromHtml(str); 
txtf.setText(text); 
txtS.setText(text); 
txtT.setText(text); 
txt1.setText(text); 
txt2.setText(text); 
txt3.setText(text); 

這是我的搜索字符串,我希望在一個ArrayList中搜索這個字符串,然後在ArrayList中獲取當我要大膽的性格。 ArrayList字符串是18758,13660,52798,16514,70679。

但是,這是用17659字符串替換所有Textview字符串與匹配字符大膽樣式。 1879年搜索字符串,這是取代舊的字符串,但我想DONOT這種類型的替代。我想大膽的風格

回答

4

以下是做this..define開始和文本的結束,並執行對部分動作的方法只有特定位置的字符變化文字.....乾杯!!!!!!!!

TextView myText = (TextView) findViewById(R.id.scoreboardtitle); 
    SpannableString text = new SpannableString("Top Scores"); 
    // make "Lorem" (characters 0 to 5) red 
    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 4, 0); 
    text.setSpan(new ForegroundColorSpan(Color.rgb(0, 255, 0)), 4, text.length(), 0); 
    text.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    myText.setText(text, BufferType.SPANNABLE); 
+0

感謝您的快速回復。是否適用於動態字符串 – vabhavsingh

+0

Ofcourse ...你需要在這裏使用邏輯,..例如 用於字符串.... SpannableString text = new SpannableString(text.getString []); for positions ... text.getLength(); 和這樣。 – jay

+0

TextView myText =(TextView)findViewById(R.id.scoreboardtitle); SpannableString text = new SpannableString(strlottery); \t //使 「的Lorem」(字符0至5)紅色 text.setSpan(新ForegroundColorSpan(Color.RED),0,4,0); (新的ForegroundColorSpan(Color.rgb(0,255,0)),4,text.length(),0); (新StyleSpan(android.graphics.Typeface.BOLD),0,1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // here crash report find myText.setText(text,BufferType.SPANNABLE); – vabhavsingh