3.17
所花时间:2小时
代码量:47
博客篇:1
了解到的知识点
EditText的焦点监听,输入错误时焦点返回该输入框
package com.example.test1; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener { private static final String TGA="aaa"; private EditText ed_id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed_id = findViewById(R.id.ex_id); EditText ed_password = findViewById(R.id.ex_password); ed_password.setOnFocusChangeListener(this); } @Override public void onClick(View v) { startActivity(new Intent(this, MainActivity2.class)); } @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){ String id = ed_id.getText().toString(); if(TextUtils.isEmpty(id) || id.length()<11){ ed_id.requestFocus(); Toast toast = Toast.makeText(MainActivity.this,"请输入11位手机号",Toast.LENGTH_SHORT); toast.show(); } } } }
就是不知道为什么Toast不能显示