[英]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.