一、前言:这是一个简单的登陆界面,两个editText,这里记录一下方便大家拿来就用。

二、上代码:

新建一个EditFocusActivity

public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {

    private EditText et_password;
    private EditText et_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_focus);
        et_phone = findViewById(R.id.et_phone);
        et_password = findViewById(R.id.et_password);
        
        et_password.setOnFocusChangeListener(this);
    }


    @Override
    public void onFocusChange(View view, boolean b) {
        String phone = et_phone.getText().toString();
        //手机号码不足11位
        if (TextUtils.isEmpty(phone) || phone.length()<11) {
            //手机号码编辑框,也就是把光标移到手机号码编辑框
            et_phone.requestFocus();
            Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
        }
    }
}

与之对应的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".edittex.EditFocusActivity">
    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入11位手机号码"
        android:inputType="number"
        android:maxLength="11"
        android:background="@drawable/edittext_selector"/>
    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入6位密码"
        android:inputType="numberPassword"
        android:maxLength="11"
        android:background="@drawable/edittext_selector"/>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"/>

</LinearLayout>

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐