How to retrieve input field content on Android?

To retrieve the content of an input box in Android, you can use the EditText widget. In Java code, you can obtain the content of EditText as follows:

EditText editText = findViewById(R.id.editText); // 找到EditText控件
String content = editText.getText().toString(); // 获取EditText中的内容

In the code above, firstly find the corresponding EditText control using the findViewById method, then use the getText method to retrieve the content in the EditText, and convert it to a string using the toString method. Now, you can access the content in the EditText using the content variable.

Additionally, you can add a TextWatcher to EditText to monitor changes in its content. For example:

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // 文本变化前的操作
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // 文本变化时的操作
    }

    @Override
    public void afterTextChanged(Editable s) {
        // 文本变化后的操作
    }
});

By adding a TextWatcher, you can perform corresponding actions when the content of an EditText changes.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds