Android 文本動態處理方法,TextView動態賦值。

來源:互聯網
上載者:User

寫在課前的話:看帖不回,胸部下垂。public void DynamicSetTextTool(int stringId, Object changeText, int viewId) {// 動態文本工具方法        String RefreshTime = getResources().getString(stringId);        String FinalRefreshTime = String.format(RefreshTime, changeText);        TextView RefreshTextObject = (TextView) findViewById(viewId);        RefreshTextObject.setText(FinalRefreshTime);

    }使用方法:DynamicSetTextTool(R.string.cancelfly,                    Integer.parseInt(SingleRoadValue[8].toString()),

                    R.id.cancelfly); 在String.xml裡要定義一個String,寫法如下:

<string name="cancelfly">取消航班:%1$d架次</string>

這個對應的就是R.string.cancelfly

%1$s是字串類型的預留位置,%1$d是數字類型的預留位置,我的方法裡使用的是Object類型,所以你傳數字或者字串都可以,自己轉化一下就可以了。

在布局檔案裡要定義一個TextView,寫法如下:
<TextView  android:textSize="19px"  android:textColor="@color/black" android:id="@+id/cancelfly" android:text="@string/cancelfly"/>
這個對應的是R.id.cancelfly 這樣就等於設定動態文本了,可以將你從WebService擷取到的值動態賦值給id為cancelfly的這個TextView。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.