2

我需要在應用程序中創建字段編輯頁面。有編輯聯繫人的EditText列表。對於每個字段類型我有一個佈局。例如 - 對於名稱和姓氏,它是浮動標籤的編輯文本。對於臉書 - 用圖標簡單編輯文字。我與Android的數據創建這個列表綁定庫Float標籤提示(TextInputLayout)不適用於Android數據綁定

我創建佈局

<data> 

    <variable 
     name="item" 
     type="app.core.db.model.Field" /> 
</data> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="65dp" 
     app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout" 
     app:theme="@style/EditFieldFloatLabeled"> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ellipsize="end" 
      android:hint="@{item.getHint()}" 
      android:imeOptions="actionNext" 
      android:maxLines="1" /> 
    </android.support.design.widget.TextInputLayout> 

</FrameLayout> 

但浮動標籤不工作。我開始逐行評論找出原因。當我評論線時,浮標變成了工作

android:hint="@{item.getHint()} 

(並用硬編碼文本替換它)。 getHint()根據字段類型返回R.string.{something}

那裏我發現比編程設置提示產生浮標籤消失。我不能使用靜態佈局(而不是回收視圖),因爲字段列表可以動態地增加(例如:我正在寫字段中的電話號碼,空白的電話字段後面插入其他電話號碼)。

有沒有辦法以同樣的方式創建浮標(通過定義提示與字段類型)?

回答

0

問題是,當沒有分配任何東西時,0將作爲資源ID給出。你可能會看到這樣的異常在你的logcat:

android.content.res.Resources$NotFoundException: String resource ID #0x0 

很可能是你沒有表達的第一次評估前分配一個值的項目。如果要確保資源未設置爲值0,則應在創建綁定後立即分配項目。

MyBinding binding = MyBinding.inflate(inflater, parent, true); 
binding.setItem(item); 
1

我有同樣的問題。

解決方案是使用TextInputLayout上的數據綁定來設置提示。

<android.support.design.widget.TextInputLayout 
    android:layout_width="match_parent" 
    android:layout_height="65dp" 
    android:hint="@{item.getHint()}" 
    app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout" 
    app:theme="@style/EditFieldFloatLabeled"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:imeOptions="actionNext" 
     android:maxLines="1" /> 
</android.support.design.widget.TextInputLayout> 
+0

修復了我的問題。但爲什麼會發生? – mitsest

相關問題