Tooth uncle tutorial is easy to understand

Results show

Interface characteristics

  • The input box uses TextInputEditText, and the hint text can be displayed at the top. If the input box is not correct, an error will be displayed at the bottom right
  • Button click to add animation

Autojs version

9.0.4

You will know the following

  • Android’S XML is almost ready-to-use
  • How does hint always display

Script summary

  • The interface is so simple, there’s nothing to say

The code on

1. Import the classes
importClass(android.animation.Animator);
importClass(android.animation.ValueAnimator);
importClass(android.animation.ObjectAnimator);
importClass(android.animation.AnimatorSet);
importClass(android.util.TypedValue);
importClass(android.text.TextUtils);
importClass(android.widget.Toast);
Copy the code
2. Interface XML, almost identical to Android
ui.layout(
  <vertical>
    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#ffffff"
    >
      <card
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="64dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="64dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="8dp"
      >
        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
        >
          <img
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="16dp"
            src="@drawable/ic_account_circle_black_48dp"
          />

          <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            android:hint="Username"
            app:expandedHintEnabled="true"
            android:textColorHint="#a2c699"
            hintColor="#fff000"
            id="userViewParent"
          >
            <com.google.android.material.textfield.TextInputEditText
              android:id="@+id/edt_user"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textColor="#9966cc"
            />
          </com.google.android.material.textfield.TextInputLayout>

          <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            android:hint="Password"
            app:expandedHintEnabled="true"
            android:textColorHint="#a2c699"
            id="pwdViewParent"
          >
            <com.google.android.material.textfield.TextInputEditText
              android:id="@+id/edt_pwd"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textColor="#9966cc"
            />
          </com.google.android.material.textfield.TextInputLayout>

          <LinearLayout
            android:layout_width="250dp"
            android:layout_height="55dp"
            android:layout_gravity="center_horizontal"
            android:gravity="center_vertical"
            android:layout_marginTop="48dp"
          >
            <card
              app:cardCornerRadius="8dp"
              android:layout_width="30dp"
              android:layout_height="45dp"
              layout_weight="1"
              margin="19"
              id="buttonParent"
            >
              <Button
                android:id="@+id/btn_login"
                android:text="Login"
                android:textColor="#fcfcfc"
                bg="#9966cc"
                bg="#ff00ff"
              />
            </card>
            <card
              app:cardCornerRadius="8dp"
              android:layout_width="30dp"
              android:layout_height="45dp"
              margin="19"
              layout_weight="1"
              id="buttonParent"
            >
              <Button
                android:id="@+id/btn_reg"
                android:text="Registered"
                android:textColor="#fcfcfc"
                bg="#9966cc"
                bg="#ff00ff"
              />
            </card>
          </LinearLayout>
        </LinearLayout>
      </card>
    </RelativeLayout>
  </vertical>
);
Copy the code
3. Create an animation
function createAnimator(view) {
  let animator1 = ObjectAnimator.ofFloat(view, "scaleX".1.1.125.1);
  let animator2 = ObjectAnimator.ofFloat(view, "scaleY".1.1.125.1);
  let animator3 = ObjectAnimator.ofFloat(view, "translationZ".0, all_2Px("4dp"), 0);
  let set = new AnimatorSet();
  set.playTogether(animator1, animator2, animator3);
  set.setDuration(300);
  return set;
}
Copy the code
4. Set click events for buttons
ui.btn_login.click(function (view) {
  log("Click the login button.");
  let set = createAnimator(view);
  set.start();
  let user = ui.edt_user.getText().toString().trim();
  let pwd = ui.edt_pwd.getText().toString().trim();
  if (TextUtils.isEmpty(user) || TextUtils.isEmpty(pwd)) {
    Toast.makeText(context, "User name or password cannot be empty", Toast.LENGTH_SHORT).show();
    return;
  }
  if ("Uncle Tooth Course".equals(user) && "123456".equals(pwd)) {
    Toast.makeText(context, "Login successful", Toast.LENGTH_SHORT).show();
  } else if (!"Uncle Tooth Course".equals(user)) {
    ui.edt_user.requestFocus();
    ui.edt_user.setError("User name error");
  } else if (!"123456".equals(pwd)) {
    ui.edt_pwd.requestFocus();
    ui.edt_pwd.setError("Password error"); }});Copy the code

The statement

This tutorial is intended for learning purposes only and is not intended for any other use