【问题标题】:Getting text from EditText wigdet by using DataBinding使用数据绑定从 EditText 小部件获取文本
【发布时间】:2020-03-11 15:40:58
【问题描述】:

我在我的项目中使用数据绑定。我了解将数据绑定到某些模型,但现在我想调用一个隐式意图以在此布局中打开谷歌地图。我称之为隐式意图的代码位于 LinearLayout 中。我不知道如何实现这一点,有人可以给我任何想法......我想在这个上使用 ViewBinding,但我不确定这是否可能。请帮忙?我没有提到我正在使用 Java。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="secondarySpinnerAdapter"
            type="android.widget.ArrayAdapter" />
        <variable
            name="secondaryClickHandler"
            type="mypackagename.MainActivity.MainActivityClickHandlers" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".MainActivity"
        tools:showIn="@layout/activity_main">

        <LinearLayout
            android:id="@+id/store_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/margin_default"
            android:orientation="horizontal"
            android:weightSum="8">

            <EditText
                android:id="@+id/et_store_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_default"
                android:layout_weight="7"
                android:hint="@string/et_store_search_hint"
                android:inputType="text"
                android:padding="@dimen/padding_default" />

            <ImageButton
                android:id="@+id/btn_store_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_default"
                android:layout_weight="1"
                android:background="@color/primary"
                android:padding="@dimen/padding_default"
                android:onClick="@{secondaryClickHandler::onSearchStoreClicked}"
                android:src="@drawable/ic_search" />
        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvShopItems"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/store_search"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
</layout>

【问题讨论】:

    标签: android data-binding android-viewbinding


    【解决方案1】:

    将值传递给视图模型中的函数。

    <ImageButton
        android:id="@+id/btn_store_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_default"
        android:layout_weight="1"
        android:background="@color/primary"
        android:padding="@dimen/padding_default"
        android:onClick="@{()->viewModel.onSearchStoreClicked(et_store_search.getText().toString())}"
        android:src="@drawable/ic_search" />
    

    查看模型功能:

    fun onSearchStoreClicked(search: String) {
       Log.d(LOG_TAG, "searchText $search");
    }
    

    【讨论】:

      【解决方案2】:

      上述解决方法可以参考这个问题:

      Android Data Binding pass arguments to onClick method

      【讨论】:

        【解决方案3】:

        毕竟我想通了。毕竟没有触及 XML 代码。我需要做的就是这个

        public void onSearchStoreClicked(View view) {
                    Intent intent =
                            new Intent(
                                    Intent.ACTION_VIEW,
                                    Uri.parse("geo:0,0?q=" + activityMainBinding
                                            .secondaryLayout
                                            .etStoreSearch.getText().toString()));
        
                    if(intent.resolveActivity(getPackageManager()) != null) {
                        startActivity(intent);
                    } else {
                        Log.d(TAG, "Cant handle this intent!");
                    }
                }
        

        【讨论】:

        • 如果其他帖子对您有帮助,您可以点赞,但不需要为您的自我回答添加噪音。
        猜你喜欢
        • 1970-01-01
        • 2021-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-01
        • 2018-10-10
        • 2019-05-12
        • 1970-01-01
        相关资源
        最近更新 更多