![](/img/trans.png)
[英]How to set a layout default value from parent layout using databinding and <include> tag in order to see it in preview?
[英]How to set the default value to the text in databinding
我有兩個編輯文本,比如說用戶名和密碼,我在它下面有一個文本視圖,在文本視圖中,用戶在我必須在其上顯示的編輯文本中輸入的內容。 假設在用戶名edittext中,如果用戶輸入shayam和密碼shree,那么在textview中我必須顯示用戶名是shayam,密碼是shree。
以上場景對我來說工作正常。 但是當用戶在 textview 中來到這個屏幕時發生了什么,它最初向我顯示 null-null ,當然這是正確的,因為在綁定模型中我最初沒有數據。
現在我的要求是:如果我沒有用戶名,那么默認情況下字符串((即在 string.xml 中提到的名稱 default_val ))我必須將 textview 設置為用戶名管理員和密碼管理員(即從字符串加載 default_val 。 xml) ,但是當用戶在 editext 中鍵入內容時,我必須顯示 generic_text 即在 string.xml 中提到的注意:我必須僅從 string.xml 中獲取值
我做了什么:當用戶名為空時,我將顯示默認字符串,否則我將顯示下面提到的通用字符串。 但為什么邏輯不起作用
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{loginModel.username ? @string/generic_text(loginModel.username , loginModel.password) : @string/default_val}"
android:gravity="center"
android:textSize="@dimen/text_size_16"/>
<string name="generic_text">Username is %1$s and Password is %2$s</string>
<string name="default_val">Username Admin and Password</string>
但是上面的代碼通過我的錯誤:
error: incompatible types: String cannot be converted to boolean
android:text="@{loginModel.username!=null ? @string/generic_text(loginModel.username, loginModel.password) : @string/default_val}"
因為要在條件中使用字符串,您必須明確提及空檢查等條件
您應該在代碼端添加它,看看它是否有效。
喜歡:
textValue = edtText1.text + "-" + edtText2
Textview.text = textValue`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.