DataBinding click

activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="viewModel"
            type="com.example.mybluetooth.MainActivity" />
        <variable
            name="onClick"
            type="android.view.View.OnClickListener" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <Button
            android:id="@+id/sppBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="36dp"
            android:layout_marginTop="28dp"
            android:onClick="@{viewModel::clickView}"
            android:text="SPP"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <Button
            android:id="@+id/sppBtn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="36dp"
            android:layout_marginTop="28dp"
            android:onClick="@{(v)->viewModel.clickView(v)}"
            android:text="SPP"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/sppBtn" />
        <Button
            android:id="@+id/sppBtn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="36dp"
            android:layout_marginTop="28dp"
            android:onClick="@{onClick}"
            android:text="SPP"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/sppBtn2" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    String TAG = MainActivity.class.getSimpleName();
    ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setViewModel(this);//將xml中定義的變量 viewModel賦值

        binding.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG,"setOnClickListener------"+view.getId());
            }
        });

        Log.e(TAG, "OnCreate");
    }

    public void clickView(View view) {
        Log.e(TAG,"clickView------"+view.getId());
        int id = view.getId();
        ...
    }
}

三種驗(yàn)證可行的click方式

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 當(dāng)你在開發(fā)一個(gè)app,通常你會(huì)有幾個(gè)版本。大多數(shù)情況是你需要一個(gè)開發(fā)版本,用來測(cè)試app和弄清它的質(zhì)量,然后還需要...
    雪殘閱讀 429評(píng)論 0 0
  • day01 英語****Generation 一代人;代(約30年),時(shí)代icon 圖標(biāo)Libraries:...
    lengol閱讀 2,057評(píng)論 0 50
  • 第二章 操作Android Studio Android Studio is ...
    青木729閱讀 907評(píng)論 2 4
  • 當(dāng)你在開發(fā)一個(gè)app,通常你會(huì)有幾個(gè)版本。大多數(shù)情況是你需要一個(gè)開發(fā)版本,用來測(cè)試app和弄清它的質(zhì)量,然后還需要...
    justCode_閱讀 523評(píng)論 0 2
  • The TODO Tool Window 待辦事項(xiàng)工具窗口 TODO means, of course, to d...
    青木729閱讀 1,427評(píng)論 0 7

友情鏈接更多精彩內(nèi)容