首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能在EditText上使用文本列表

不能在EditText上使用文本列表
EN

Stack Overflow用户
提问于 2017-04-08 11:45:19
回答 2查看 39关注 0票数 0

当与TextView相关的EditText为空时,我试图隐藏它。为了检查它是否为空,我想使用TextChangedListner,就像我在其他帖子中看到的那样,但不知怎么的,我没有这个选项。当我使用addTextChangedListner时,我得到了Cannot resolve symbol 'addTextChangedListen'的警告。

我还包括了EditTextTextView

代码语言:javascript
运行
复制
import android.widget.EditText;
import android.widget.TextView;

public class Register extends AppCompatActivity {

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

    //first name
    EditText firstnameBox = (EditText) findViewById(R.id.firstnameText);
    firstnameBox.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged() {
            //here i'm going to see if the firstnameBox is empty or not
            ...
        }
    });
}

如何使用文本更改的Listner?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-08 11:47:19

OnCreate方法之后在setContentView之后移动下面的行

代码语言:javascript
运行
复制
    //first name
EditText firstnameBox = (EditText) findViewById(R.id.firstnameText);
firstnameBox.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged() {
        //here i'm going to see if the firstnameBox is empty or not
        ...
    }
});
票数 0
EN

Stack Overflow用户

发布于 2017-04-08 11:53:35

您必须以可执行的方式编写代码。

editText.addTextChangeListener代码写入onCreate()方法。

代码语言:javascript
运行
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_register);

   EditText firstnameBox = (EditText) findViewById(R.id.firstnameText);
   firstnameBox.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged() {
            //here i'm going to see if the firstnameBox is empty or not
            ...
        }
    });
}

有关更多信息,请查看此安卓活动LifeCycle

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43293678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档