我想在Spinner中修剪用户的选择。当用户选择他的国家(例如“+232 Sierra Leone”)时,当选择项目并且微调器关闭时,仅显示“+232”。
提前感谢您的建议。
// I have two arrays of String that i join to make one
String[] phoneCodes ={"232", "44", "1"};
String[] countries = {"Sierra Leone", "United Kingdom", "United States"};
String[] phoneCodesCountries = new String[3];
// Here i join those two arrays, for example "+232 Sierra Leone"
for (int i = 0; i < phoneCodes.length; i++) {
phoneCodesCountries[i] = "+" + phoneCodes[i] + " " + countries[i];
}
// Setting the adapter to phoneCodesArray
ArrayAdapter<String> phoneCodesArray = new ArrayAdapter<String>(RegisterActivity.this, android.R.layout.simple_spinner_item, phoneCodesCountries);
phoneCodesArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spPhoneCodes.setAdapter(phoneCodesArray);
答案 0 :(得分:1)
您可以通过
获取微调器mySpinner.getSelectedItem().toString();
所以在你的例子中,价值将是+232塞拉利昂。现在,您可以通过拆分整个字符串来获取国家/地区代码:
String[] split = mySpinner.getSelectedItem().toString().split("\\+");
现在split [0]将得到你想要的结果+232。