我想帮助解决问题。
首先,按照我的代码的详细信息:
build.gradle (Project: android)
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url '/home/melti/java/repository' }
}
dependencies {
classpath "com.android.tools.build:gradle:1.3.0-beta4"
classpath "com.android.databinding:dataBinder:1.0-rc0"
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url '/home/melti/java/repository' }
}
}
build.gradle(Module:app)
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc2"
defaultConfig {
applicationId "br.com.soma"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:recyclerview-v7:21.0.3'
compile 'com.android.support:cardview-v7:21.0.3'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'org.springframework:spring-core:4.1.7.RELEASE'
compile 'org.apache.commons:commons-io:1.3.2'
}
AmanteEditModel
package br.com.soma.amante.edit;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import br.com.soma.BR;
/**
* Created by spassu on 09/07/15.
*/
public class AmanteEditModel extends BaseObservable {
private String senhaConfirm;
@Bindable
public String getSenhaConfirm() {
return senhaConfirm;
}
public void setSenhaConfirm(String senhaConfirm) {
this.senhaConfirm = senhaConfirm;
notifyPropertyChanged(BR.senhaConfirm);
}
}
fragment_amante_edit
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto">
<data>
<variable name="model" type="br.com.soma.amante.edit.AmanteEditModel"/>
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/card_edit_style"
android:layout_gravity="top"
card_view:cardCornerRadius="0dp">
<EditText
android:id="@+id/amante_edit_senha_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="Confirme a senha"
android:inputType="textPassword"
android:maxLines="1"
android:text="@{model.senhaConfirm}"
/>
</android.support.v7.widget.CardView>
</ScrollView>
</layout>
AmanteEditFragment
package br.com.soma.amante.edit;
import android.app.Fragment;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import br.com.soma.R;
import br.com.soma.amante.view.AmanteViewActivity;
import br.com.soma.databinding.FragmentAmanteEditBinding;
/**
* Created by spassu on 27/05/15.
*/
public class AmanteEditFragment extends Fragment {
private AmanteEditModel model;
private static final String AMANTE_ID = "amanteId";
public static final String DEFAULT_FRAGMENT_TAG = "amanteEditFragment";
// Views
private long amanteId;
public AmanteEditFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
model = new AmanteEditModel();
FragmentAmanteEditBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_amante_edit, container, false);
binding.setModel(model);
return binding.getRoot();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_amante_edit, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
// The Save button was pressed
case R.id.menu_amante_edit_save:
save();
getActivity().finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public static AmanteEditFragment newInstance(long id) {
AmanteEditFragment fragment = new AmanteEditFragment();
Bundle args = new Bundle();
args.putLong(AmanteViewActivity.EXTRA_AMANTEID, id);
fragment.setArguments(args);
return fragment;
}
private void save() {
Toast.makeText(getActivity(), model.getSenhaConfirm(), Toast.LENGTH_SHORT).show();
}
}
在senhaConfirm
中输入数据并点击save(
后),我发现绑定不起作用,即senhaConfirm
到AmanteEditModel
尚未生效。谁能帮我?
答案 0 :(得分:5)
编辑:据Remi David称,Android Studio现在具有双向数据绑定功能。我在下面描述的解决方案应该不再是必需的。
感谢Remi指出这一点。
关于用户输入,Android Databinding只是一种方式。 GUI将自动反映模型中的任何更改,但反之亦然。
您需要向模型类添加TextChangeListener,以便在用户更改任何内容时设置模型属性。
示例:
public class AmanteEditModel extends BaseObservable {
private String senhaConfirm;
@Bindable
public String getSenhaConfirm() {
return senhaConfirm;
}
public void setSenhaConfirm(String senhaConfirm) {
this.senhaConfirm = senhaConfirm;
notifyPropertyChanged(BR.senhaConfirm);
}
// Textwatcher Reference: http://developer.android.com/reference/android/text/TextWatcher.html
public TextWatcher getMyEditTextWatcher() {
return new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// Important! Use the property setter, otherwhise the model won't be informed about the change.
setSenhaConfirm(s);
}
};
}
}
在你的布局中,xml将EditText更改为:
<EditText
android:id="@+id/amante_edit_senha_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:hint="Confirme a senha"
android:inputType="textPassword"
android:maxLines="1"
android:text="@{model.senhaConfirm}"
app:addTextChangeListener="@{model.myEditTextWatcher}"
/>
注意 addTextChangeListener 的命名空间。这个方法可能无法通过android:namespace获得,所以我使用app:here。您也可以使用bind:使绑定更清晰。
所以不要错过添加
xmlns:app="http://schemas.android.com/apk/res-auto"
或
xmlns:bind="http://schemas.android.com/apk/res-auto"
到您的XML名称空间。
此解决方案适用于所有输入控件,包括自定义,因为您在模型中提供了正确的监听器。