[英]What is difference between these two code blocks?
在 Android Studios 上运行,要求用户输入信息。 在编辑文本中,然后在单击提交按钮后在文本视图上显示相同的内容,这些行写在按下按钮时调用的方法中。 这些值已在 XML 文件中定义
fNameTextView="First Name :" ,
lNameTextView="Last Name :" ,
emailTextView="eMail Id :",
phoneNumberTextView="Phone Number :"
看这段代码:
public void WhenButtonClicked(){
//function called when button is clicked
fNameTextView.setText("First Name :"+fNameEditText.getText().toString()); //text view to display fname
// What is the difference between above block and below block of code?
fNameTextView.setText(fNameTextView.getText()+fNameEditText.getText().toString()); //textview to display
}
这两个代码语句有什么区别?
不同之处在于,第一个示例中的值是硬编码字符串,而第二个示例中,它获取配置的值。
它目前可能会产生相同的 output,但如果您更改了fNameTextView
的配置,如果您希望它匹配,您还需要更新第一个代码语句。
使用第二个代码语句,它将“正常工作”。 更少的维护,更少的重复,更多的一致性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.