2016-04-18 7 views
6

ich einen einfachen Login-Bildschirm mit der Datenbindung erstellt habe aufzurufen, wie bei http://developer.android.com/tools/data-binding/guide.html jedoch beschrieben Ich bin nicht in der Lage die Benachrichtigung von Text ausAndroid Datenbindung - nicht in der Lage das die Click-Handler

geändert bekommen
  1. das Textfeld oder
  2. der Knopf klicken.

Jede Hilfe ist willkommen

ich für das Textfeld Benachrichtigung denken, könnte das Android-Team nicht haben es vollständig umgesetzt. Allerdings verstehe ich meinen Fehler für den Button-Click-Handler nicht.

Das Fragment Code sieht wie

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    FragmentAccountSetupInitialBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_account_setup_initial, container, false); 
    View view = binding.getRoot(); 

    binding.setAccount(new UserAccount()); 
    return view; 
} 

Das Ansichtsmodell für Benutzerkonto wird wie unten definiert

public class UserAccount extends BaseObservable { 
    private String _email; 
    private String _password; 

    @Bindable 
    public String getEmail() { 
     return _email; 
    } 

    public void setEmail(String email) { 
     if(!TextUtils.equals(_email, email)) { 
      _email= email; 
      notifyPropertyChanged(BR.email); 
     } 
    } 


    @Bindable 
    public String getPassword() { 
     return _password; 
    } 

    public void setPassword(String password) { 
     if(!TextUtils.equals(_password, password)) { 
      _password = password; 
      notifyPropertyChanged(BR.password); 
     } 
    } 

    public void onSignInButtonClick(View view) { 
     // Sign in now 
    } 
} 

Und das Fragment-Layout ist

<layout 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <data> 
     <variable name="account" type="project.namespace.UserAccount"/> 
    </data> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingTop="56dp" 
      android:paddingLeft="24dp" 
      android:paddingRight="24dp"> 

      <!-- Email Label --> 
      <android.support.design.widget.TextInputLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="8dp" 
       android:layout_marginBottom="8dp"> 
       <android.support.design.widget.TextInputEditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textEmailAddress" 
        android:hint="@string/email" 
        android:text="@{account.email}"/> 
      </android.support.design.widget.TextInputLayout> 

      <!-- Password Label --> 
      <android.support.design.widget.TextInputLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="8dp" 
       android:layout_marginBottom="8dp"> 
       <android.support.design.widget.TextInputEditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textPassword" 
        android:hint="@string/password" 
        android:text="@{account.password}"/> 
      </android.support.design.widget.TextInputLayout> 


      <android.support.v7.widget.AppCompatButton 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="24dp" 
       android:layout_marginBottom="24dp" 
       android:padding="12dp" 
       android:text="@string/signin" 
       android:onClick="@{account.onSignInButtonClick}"/> 

     </LinearLayout> 

    </FrameLayout> 
</layout> 

aktualisieren

Umzug auf Android Studio 2.1 Beta 2 löst das erste Problem. Aktualisiert das Layout als

<android.support.design.widget.TextInputEditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textEmailAddress" 
    android:hint="@string/email" 
    android:text="@={account.email}"/> 
+0

gehen Sie durch diesen Link [onClick Handler] (http://stackoverflow.com/questions/31961901/ using-databinding-library-for-binding-events) können Sie die Lösung für diese Frage finden. –

+0

Ich denke, genau das mache ich. Statt einer separaten MyHandler-Klasse behandle ich sie in der UserAccount-Klasse. – resp78

+1

Datenbindung ist derzeit nur "ein Weg". Nur Änderungen in Ihrem Modell benachrichtigen die Ansicht - nicht umgekehrt. Dies ist eine Funktion, die mit "Android Studio 2.1" geliefert wird ([Quelle] (https://halfhoethout.wordpress.com/2016/03/23/2-way-data-binding-on-android/?utm_source=androiddevdigest). Ihr Code sieht gut aus, können Sie Ihr Problem besser erklären? Ruft Ihre OnClick-Methode auf? – yennsarah

Antwort

1

folgt Wie ich bekannt, scheint das OnClick-Ereignis nicht in Fragment arbeiten und ich weiß nicht BindingAdapter Anmerkung definieren benutzerdefinierte Setter why.Try zu verwenden.

+0

yep, gleich hier. – beerBear

2

Können Sie Folgendes für Ihre Schaltfläche ausprobieren?

<android.support.v7.widget.AppCompatButton 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="24dp" 
      android:layout_marginBottom="24dp" 
      android:padding="12dp" 
      android:text="@string/signin" 
      android:onClick="@{account::onSignInButtonClick}"/> 

ich einen Fehler auf dem IDE erhalten ('!=', '%'... expected, got ':'), aber es funktioniert, wenn die App läuft ... Der wichtigste Teil ist die „::“.

Hoffe das hilft dir!