【问题标题】:Android: two edittext changeable, error on the second [closed]Android:两个edittext可变,第二个错误[关闭]
【发布时间】:2012-12-04 13:42:53
【问题描述】:

我有这个代码:

    final EditText kcalh=(EditText) findViewById(R.id.kcalh_inserisci);
    final EditText kw=(EditText) findViewById(R.id.kw_inserisci);
    kcalh.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
        public void afterTextChanged(Editable arg0) {
            int risultato=Integer.parseInt(kcalh.getText().toString())/860;
            kw.setText(""+risultato);
        }
        public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) {}
     });
    kw.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
        public void afterTextChanged(Editable arg0) {
            int risultato=Integer.parseInt(kw.getText().toString())*860;
            kcalh.setText(""+risultato);
            System.out.println(risultato);
        }
        public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) {}
     });

当我更改第一个编辑文本时,一切正常(第二个编辑文本更改它的值...当我使用第二个时应用程序崩溃。 你知道为什么? 我认为它会进入第一个的后文并崩溃……但为什么不在第二个中呢?

【问题讨论】:

  • eroor 日志显示什么?
  • 发布该死的日志猫。为什么不明显?!
  • 因为不需要,这是一个明显的错误。和 logcat 它太长了。我已经解决了将 .hasFocus 放入 afterTextChanged
  • 您是否真的发布了一个问题,然后当有人要求发布 logcat 时抱怨这是一个明显的错误?

标签: android android-edittext onchange


【解决方案1】:

在将 String 解析为 Integer 之前检查 EditText 是否为空:

kw.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
        public void afterTextChanged(Editable arg0) {
            if(kw.getText().toString().length() > 0){
              int risultato=Integer.parseInt(kw.getText().toString())*860;
               kcalh.setText(""+risultato);
               System.out.println(""+risultato);
           }
        }
        public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) {}
     });

【讨论】:

    猜你喜欢
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    相关资源
    最近更新 更多