dataBinding { enabled true } or dataBinding { enabled true } buildFeatures { dataBinding = true } Convert to data binding layout <data> <variable name="data" type="com.example.demo.MyViewModel" /> </data>
<TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{String.valueOf(data.count)}" android:textColor="#3F51B5" android:textSize="26sp" app:layout_constraintBottom_toBottomOf="@+id/guideline2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.549" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-1" android:onClick="@{()->data.changeCount(-1)}" app:layout_constraintBottom_toTopOf="@+id/guideline3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@+id/guideline" app:layout_constraintTop_toTopOf="@+id/guideline2" />
public class MainActivity extends AppCompatActivity { MyViewModel myViewModel; ActivityMainBinding binding;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); }
private void init() { binding = DataBindingUtil.setContentView(this, R.layout.activity_main); myViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MyViewModel.class); binding.setData(myViewModel); binding.setLifecycleOwner(this); } }
myBroadcastReceiver = new MyBroadcastReceiver(); registerReceiver(myBroadcastReceiver, new IntentFilter("haha"));
Intent intent = new Intent("haha"); sendBroadcast(intent);
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "show", Toast.LENGTH_SHORT).show(); } }
|