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" />

posted @ 2024-03-24 17:52  赵千万  阅读(3)  评论(0编辑  收藏  举报