Android动态语言字符串

时间:2014-04-15 18:37:44

标签: android

我宣布了一个字符串

 <string name="body_activity_welcome">Hello! <br />This is the welcome page. Only logged in users can see this page.</string>

但我想亲自问候用户

 <string name="body_activity_welcome">Hello {$username}! <br />This is the welcome page. Only logged in users can see this page.</string>

有没有办法在获取数据时将数据传递给语言字符串?

    TextView text = (TextView) findViewById(R.id.register_status_message);
    text.setText(R.string.body_activity_welcome); // somehow pass username to the string?

1 个答案:

答案 0 :(得分:2)

您应该使用getString(),并将{$username}替换为%1$s

其中

  

%1:是参数编号

     

$ s:是类型

getString的第一个参数是资源ID,然后你有一个varargs,它是你要传递给字符串的所有数据。

getString(R.string.body_activity_welcome, userName);

TextView text = (TextView) findViewById(R.id.register_status_message);
text.setText(getString(R.string.body_activity_welcome, username)); //