一、前言:这是一个简单的登陆界面,两个editText,这里记录一下方便大家拿来就用。
二、上代码:
新建一个EditFocusActivity
public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {private EditText et_password;private EditText et_phone;@Overrideprotected 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);}@Overridepublic 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"><EditTextandroid: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"/><EditTextandroid: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"/><Buttonandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="登录"/></LinearLayout>