关于Android Studio中的EditText监听器,TextWatcher不起作用。

huangapple go评论101阅读模式
英文:

about anroidstudio edittext listener, TextWatcher doesn't work

问题

new TextWatcher 意味着红色下划线... 为什么?
我找到了一种使用 TextWatcher 的方法,通过浏览博客。
大多数人都像这样使用。
我漏掉了什么吗?
另外,我想学习 Android 编程,但我只懂 C 语言。
我是否需要先学习 Java?

package org.techtown.p4;

import android.os.Bundle;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
    EditText editText;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText=(EditText)findViewById(R.id.editTextTextMultiLine);
        textView=(TextView)findViewById(R.id.textView1);

        String string=editText.getText().toString();
        int cnt=string.length();

        textView.setText(""+cnt);

        editText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                String string=editText.getText().toString();
                int cnt=string.length();

                textView.setText(""+cnt);
            }
        });
    }
}
英文:

new TextWatcher implies red underline... why?
I found way to use TextWatcher by surffing blogs.
Most of people use like that.
What am I missing?
Additionally, I wish to learn android programming but, I only know C language.
Do I have to study java first?

package org.techtown.p4;

import android.os.Bundle;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
    EditText editText;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText=(EditText)findViewById(R.id.editTextTextMultiLine);
        textView=(TextView)findViewById(R.id.textView1);

        String string=editText.getText().toString();
        int cnt=string.length();

        textView.setText(""+cnt);

        editText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                String string=editText.getText().toString();
                int cnt=string.length();

                textView.setText(""+cnt);
            }
        });
    }
}

答案1

得分: 1

TextWatcher是一个接口,您应该实现接口中定义的所有方法。如果您使用Android Studio,在TextWatcher上按下ALT+INSERT,IDE将提示您要做什么。

editText.addTextChangedListener(new TextWatcher()
{
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
});
英文:

TextWatcher is an interface, you should implement all the methods defined in the interface. If you use android studio, press ALT+INSERT on the TextWatcher, The IDE will prompts you what to do.

editText.addTextChangedListener(new TextWatcher()
{
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
});

答案2

得分: 0

field1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}

@Override    
public void beforeTextChanged(CharSequence s, int start,
 int count, int after) {
}

@Override    
public void onTextChanged(CharSequence s, int start,
 int before, int count) {
  if(s.length() != 0)
    field2.setText("");
}

});

英文:

I think its because you have not implemented other two methods which is provided by TextWatcher Class
Please check below code

field1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}

@Override    
public void beforeTextChanged(CharSequence s, int start,
 int count, int after) {
}

@Override    
public void onTextChanged(CharSequence s, int start,
 int before, int count) {
  if(s.length() != 0)
    field2.setText("");
}
});

huangapple
  • 本文由 发表于 2020年8月27日 13:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63609941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定