清水鼻涕泡 2017-09-05 13:06 采纳率: 100%
浏览 1123
已采纳

安卓对Edittext监听的问题

 public class MainActivity extends ActionBarActivity {
    private EditText first;
    private EditText second;
    private TextView result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        first =(EditText) findViewById(R.id.first);
        second=(EditText) findViewById(R.id.second);
        result=(TextView) findViewById(R.id.result);
        first.addTextChangedListener(textwatcher);
        second.addTextChangedListener(textwatcher);
    }
    private TextWatcher textwatcher =new TextWatcher() {
        int f=Integer.parseInt(first.getText().toString());
        int s=Integer.parseInt(second.getText().toString());
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            result.setText(f+s);
        }
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
        }
    };

为什么编译时没有显示错误 但运行在模拟器上确程序出错停止运行了呢(XML文件是对的)

  • 写回答

3条回答

  • ziyejinwei1994 2017-09-06 02:42
    关注

    除了判断楼上的方法,还有 result.setText(f+s); 你写成 result.setText(f+s+“”);转换成字符串

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大