DataBinding click

activity_main.xml




    

        
        
    

    
        

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();
        ...
    }
}

三种验证可行的click方式

你可能感兴趣的:(DataBinding click)