3.24
所花时间(包括上课):2.5
打码量(行):230
博客量(篇):1
了解到知识点:学习焦点变更监听器
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
editText.setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.getId() == R.id.edit_text) {
if (hasFocus) {
// EditText 获得焦点时执行的操作
Toast.makeText(MainActivity.this, "EditText focused", Toast.LENGTH_SHORT).show();
} else {
// EditText 失去焦点时执行的操作
Toast.makeText(MainActivity.this, "EditText focus lost", Toast.LENGTH_SHORT).show();
}
}
}
}
<!-- activity_main.xml -->
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text" />
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/18086430
千万千万赵千万