【问题标题】:Android spinner get text after commaAndroid微调器在逗号后获取文本
【发布时间】:2014-06-23 08:39:11
【问题描述】:

在我的 android 应用程序中,我使用微调器来选择数据。我为要在微调器中显示的字符串创建了字符串数组。我将所有详细信息放在字符串文件夹中。一旦用户选择了项目,我希望所选文本显示在编辑文本中。..

例如:微调器用于选择国家代码假设用户选择了美国 那么选中的文字会是这样的

美国,+001

我不需要获取所有文本并将其显示在编辑文本中。我只需要逗号后面的文字,即+001。那么有没有办法只获取逗号后的文本

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();

我知道这将显示所有文本我只想要逗号后显示的文本

【问题讨论】:

  • 不能使用拆分?像这样:"text.split(',')[1]" ?

标签: android string spinner country


【解决方案1】:

你可以用逗号分割你的文本:

String text = spinner.getSelectedItem().toString();
String[] splited_text = text.split(",");
text = splited_text[1];

【讨论】:

  • text = splited_text[1];
【解决方案2】:

假设文本位于字符串名称text 中。使用这个:

String[] temp = text.split(",")
String code  = temp[1]; //+001 the code after , temp[0] contains the rest

【讨论】:

    【解决方案3】:
    String seperated[] = spinner.getSelectedItem().toString().split(",");
    text = seperated[1];
    

    这将只返回“+001”。

    【讨论】:

      【解决方案4】:
      String code = text.substring(text.indexOf(','));
      

      【讨论】:

        【解决方案5】:

        这是在最后一个逗号之后获取字符串的方法:

                String founded;
                Pattern p = Pattern.compile(".*,\\s*(.*)");
                String dd = (String) "Your string, really, really2";
                Matcher m = p.matcher(dd);
        
                if (m.find()) {
                    founded = m.group(1); //returns "really2"
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-18
          • 1970-01-01
          • 2020-10-11
          • 1970-01-01
          • 1970-01-01
          • 2015-12-10
          • 2011-08-12
          相关资源
          最近更新 更多