问答详情
源自:2-2 Data Binding基础用法

为什么除了监听器绑定会提示程序包不存在,找不到符号,别的都行呢,语法应该没错,我贴下代码

public void  OnClickListenerBinding(User user){
    Toast.makeText(MainActivity.this,user.getLastName(),Toast.LENGTH_SHORT).show();
}
<TextView
    android:onClick="@{() -> presenter.OnClickListenerBinding(user)}"
    android:text="@{User.lastName}"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
只有绑定监听器事件不行,别的都可以的,这是什么原因,语法检查很多遍没错啊


提问者:慕粉4142398 2016-10-30 22:15

个回答

  • feels_chaotic
    2016-11-01 10:39:57
    已采纳

    http://img.mukewang.com/5818006200014b4606180073.jpg所以到底是User还是user呢? 建议贴全代码

  • 慕粉4142398
    2016-11-01 17:40:04

    public class MainActivity extends AppCompatActivity {
    
        User user = new User("小白","小黑");
        private ActivityMainBinding binding;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    //        binding.firstName.setText(user.getFirstName());
    //        binding.lastName.setText(user.getLastName());
            binding.setUser(user);
    //        binding.setVariable(BR.User,user);
          binding.setPresenter(new Presenter());
    
    
    
        }
    
        public class Presenter{
            public void onTextChanged(CharSequence s,int start,int before,int count){
                user.setFirstName(s.toString());
                binding.setUser(user);
            }
    
            public void onClick(View view){
                Toast.makeText(MainActivity.this,"点到了",Toast.LENGTH_SHORT).show();
            }
              public void  OnClickListenerBinding(User user){
                  Toast.makeText(MainActivity.this,user.getLastName(),Toast.LENGTH_SHORT).show();
              }
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
       >
        <data>
           <variable
               name="User"
               type="chachewuyou.qs.demo.User"/>
            <variable
                name="presenter"
                type="chachewuyou.qs.demo.MainActivity.Presenter"/>
        </data>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">
    
            <EditText
                android:onTextChanged="@{presenter.onTextChanged}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="输入First Name" />
    
            <EditText
    
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="输入Last Name" />
    
            <TextView
                android:onClick="@{presenter.onClick}"
                android:text="@{User.firstName}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <TextView
                android:onClick="@{() -> presenter.OnClickListenerBinding(user)}"
                android:text="@{User.lastName}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
    </layout>