如何在concat';用Java编程?
因此,我按照文档将XML字符串值与其他内容连接在一起,因此产生了以下代码:如何在concat';用Java编程?,java,android,xml,string,concatenation,Java,Android,Xml,String,Concatenation,因此,我按照文档将XML字符串值与其他内容连接在一起,因此产生了以下代码: //XML strings.xml <string name="start_next_act_string">The next activity has been scheduled to automatically launch in %1$d seconds!</string> //layout/activity_home.XML <TextView andro
//XML strings.xml
<string name="start_next_act_string">The next activity has been scheduled to automatically launch in %1$d seconds!</string>
//layout/activity_home.XML
<TextView
android:text="@string/start_next_act_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//Home.java inside class, but outside oncreate
int startNextActivityInSeconds = 10;
//Home.java inside class and inside oncreate
super.onCreate(savedInstanceState);
Resources res = getResources();
String text = String.format(res.getString(R.string.start_next_act_string), startNextActivityInSeconds);
setContentView(R.layout.activity_home);
//XML strings.XML
已计划在%1$d秒内自动启动下一个活动!
//布局/活动\u home.XML
//java在类内部,但在oncreate外部
int startNextActivityInSeconds=10;
//java内部类和内部oncreate
super.onCreate(savedInstanceState);
Resources res=getResources();
String text=String.format(res.getString(R.String.start\u next\u act\u String),startNextActivityInSeconds);
setContentView(R.layout.activity_home);
但是在Android屏幕上,它会打印出来
已计划在%1$d秒内自动启动下一个活动
而不是
已计划在10秒内自动启动下一个活动
如何更新XML字符串,使其显示concat的字符串结果?恐怕您尝试的操作不起作用。当布局膨胀时,字符串的使用与资源文件中的定义完全相同 为此,必须显式设置文本,即
setContentView(R.layout.activity_home);
TextView tv = (TextView)findViewById(R.id.theId);
tv.setText(text);
其中“theId”是必须在xml文件中为TextView设置的id,例如:
<TextView
android:id="@+id/theId"
android:text="@string/start_next_act_string"
...
您需要将TextView
的文本属性设置为您创建的新字符串,否则格式化的字符串将单独存储在本地字符串中,不会反映在UI中
首先,您需要为TextView命名一个ID:
<TextView
android:id="@+id/YourId"
...
TextView myTextView = (TextView)findViewById(R.id.YourId);
myTextView.setText(text);