XiaoShou111
log4lgb
采纳率28.6%
2018-03-03 08:37 阅读 2.8k

Android DataBinding 当中,如何传入方法参数

5
 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="cityModel"
            type="com.li_xyz.mvvm.viewmodel.CityModel" />

    </data>

    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp">

        <EditText
            android:id="@+id/cityName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/cityName"
            android:onClick="这里该怎么写?"
            android:text="查询" />

    </RelativeLayout>
</layout>

CityModel 当中有一个 search(String cityName) 方法

想要实现的结果是,点击按钮,调用 search 方法,同时将 EditText 输入的内容作为参数传入

Button 的 android:onClick 该怎么写呢?

DataBinding 刚学了一点儿皮毛,不是很了解,请大神赐教

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    z979451341 键盘舞者113 2018-03-03 08:40
     <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <data>
    
            <variable  name="viewModel" type="com.mmlovesyy.bindingadapterdemo.NamesViewModel" />
        </data>
    
        <LinearLayout  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin">
    
            <Button  android:id="@+id/add_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@{viewModel.onClick}" android:text="+" />
    
            <LinearLayout  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:context="@{viewModel.context}" app:names="@{viewModel.names}"></LinearLayout>
    
    
        </LinearLayout>
    
    
    </layout>
    
     public class MainActivity extends AppCompatActivity {
    
        private NamesViewModel viewModel = new NamesViewModel(this);
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
            binding.setViewModel(viewModel);
        }
    }
    
    点赞 评论 复制链接分享
  • qq_38074369 wkp111 2018-03-05 03:02

    @{() -> cityModel.search(cityName)}

    点赞 评论 复制链接分享

相关推荐