xml資源getStringArray(R.array.xxx)方法,r.array

來源:互聯網
上載者:User

xml資源getStringArray(R.array.xxx)方法,r.array

在res/values/下建立menu_names.xml

代碼如下:

<?xml version="1.0" encoding="utf-8"?><resources>     <string-array name="menu_names">        <item>xx1</item>                 <item>xx2</item>                 <item>xx3</item>              </string-array></resources>

onCreate(Bundle savedInstanceState) 方法中:

String[] stringArray = getResources().getStringArray(R.array.menu_names);        menuList = new ArrayList<String>();        for (String str : stringArray) {            menuList.add(str);        }

擷取xml裡的值


Android中getContext()getResources()getStringArray()起什作用?

getResources().getStringArray(R.array.setting);通過這段代碼可以獲得以下xml檔案裡的資源
<resources>
<string-array name="setting">
<item>查看通話明細</item>
<item>發送資訊</item>
<item>添加到快速撥號</item>
<item>刪除</item>
<item>同號刪除</item>
<item>刪除全部通話記錄</item>
<item>添加到黑名單</item>
<item>刪除黑名單</item>
</string-array>
<resources>
 
getStringArray(String key) 的問題

Inaccountinfo.java:
Intent intent = new Intent(Inaccountinfo.this,InfoManage.class); //建立Intent對象
intent.putExtra(“FLAG”,new String[]{“strid”,“strType”}); //傳遞資料
startActivity(intent); //執行intent操作

InfoManage.java:
Intent intent = getIntent(); // 擷取Intent對象
Bundle bundle = intent.getExtras(); //擷取傳人的資料,並使用Bundle記錄
//!!!
strinfos = bundle.getStringArray("FLAG");
for (int i = 0; i < strinfos.length; i++) {

System.out.println(strinfos[i]);
}

把你寫的程式稍微變動一下,最後輸出的結果是“strid”,“strType” 。具體你想要表達什麼,我不太清楚,這個是測試過後的結果。向這類問題,你可以自己在eclipse中測試,我估計你是剛學習android,如果是這樣的話,建議多動手,多看看developers.androidcn.com/reference/packages.html.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.