英文:
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("");
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论