我在过去的几个月里一直在通过Udacity学习android开发,并遇到了一个奇怪的问题,这个问题没有很好地解释,希望我能够得到一些见解。对不起,我不知道所有与我有关的Java语言。为什么我的方法不会显示该值,除非在我的strings.XML中添加了一个XML占位符? Android开发
当我将所有字符串添加到strings.XML文件时,我遇到了一个问题。我正在研究支持不同语言的设置。我浏览了xliff的一些文档,该部分用于标识不需要翻译的字符串部分。 %s(占位符?)部分是我没有经验的部分,它似乎改变了我编写我的java代码的方式。
这是我为这个问题制作的应用程序的代码。
这是我的strings.xml我注释掉XLIFF:摹改变我的Java代码的行为,我将解释后
Test Application
customer name is:
activity_main.xml中
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.android.testapplication.MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Display Customer Name" />
android:id="@+id/customer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"/>
android:id="@+id/display_name_here"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
android:id="@+id/display_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Text"
android:onClick="submitText"/>
MainActivity.Java
package com.example.android.testapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//Display Message Method
private void displayMessage(String message) {
TextView displayTextView = (TextView) findViewById(R.id.display_name_here);
displayTextView.setText(message);
}
//Submit Text Method
public void submitText (View view) {
EditText editViewCustomerName = (EditText) findViewById(R.id.customer_name);
String customerName = editViewCustomerName.getText().toString();
String customMessage = createOrderSummary(customerName);
displayMessage(customMessage);
}
//Create order summary that only has the customer name
private String createOrderSummary(String addCustomerName) {
String orderMessage = getString(R.string.order_summary_name, addCustomerName);
return orderMessage;
}
}
这是我使用的字符串,当我单击显示文本时,会在手机上显示客户的姓名。这是教师让我们写出来的内容,但没有解释您可以用逗号分隔R.id字符串,然后调用addCustomerName以显示您输入的名称。
教师代码更改即不同于矿山
String orderMessage = getString(R.string.order_summary_name, addCustomerName);
这是我收到了它和它的工作方式。这是我的一段代码
String orderMessage = getString(R.string.order_summary_name) + addCustomerName;
但是这是我得到真正困惑。如果我在我的Strings.XML文件中注释掉我的xliff(就像我已经拥有它),我不能使用教师代码将逗号分隔开,并用逗号将其保留在括号内。我必须像上面那样连接它。
我周围搜索,因为我是新来的,我不知道该找什么。我发现的唯一的事情就是删除%s(这是一个XML占位符?)它使用教师代码破坏了它。但是,当我把它放回去有效?
如果有人能解释这一点,将不胜感激。我被困在这个好几天了。
下面是使用教师代码时,在我的设备上显示的一些屏幕截图。
从我XLIFF上的strings.xml
中删除添加%s%S回琴弦上XLIFF。XML