结果
E/username+password: xxxxxxxxx+yyyyyyyyyyyyy
直接上源码吧
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定工作
ActivityLoginBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_login);
LoginViewModel loginViewModel = new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(LoginViewModel.class);
//绑定工作
binding.setLoginvm(loginViewModel);
//建立感应
binding.setLifecycleOwner(this);
//把他当作初始化函数使用更方便,比如要用个列表
loginViewModel.setBinding(binding,this);
}
}
LoginViewModel.java
public class LoginViewModel extends AndroidViewModel {
private static ActivityLoginBinding binding;
@SuppressLint("StaticFieldLeak")
private static LoginActivity loginActivity;
public MutableLiveData<String> _username=new MutableLiveData<>();
public MutableLiveData<String> _password=new MutableLiveData<>();
public LoginViewModel(@NonNull Application application) {
super(application);
_username.setValue("");
_password.setValue("");
}
public void setBinding(ActivityLoginBinding binding, LoginActivity loginActivity) {
//把binding和mainActivity都赋值给MainVM作为静态变量备用,因为很多绑定的控件都只能用静态方法
LoginViewModel.binding =binding;
LoginViewModel.loginActivity =loginActivity;
}
public void Login(){
String username = _username.getValue();
String password = _password.getValue();
Log.e("username+password",username+"+"+password);
}
}
activity_login.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">
<data>
<variable
name="loginvm"
type="cn.titansys.konect.vm.LoginViewModel" />
</data>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.LoginActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:text="@={loginvm._username}"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:text="@={loginvm._password}"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:onClick="@{()->loginvm.Login()}"
android:text="登 陆"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</layout>
bulid.gradle
android {
compileSdk 31
defaultConfig {
applicationId "cn.xxxx.xxx"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
dataBinding{
enabled true
}
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures{
dataBinding = true // for data binding
viewBinding = true // for view binding
}
}