无法解析setText方法

时间:2017-03-24 01:33:47

标签: android

这是该程序。它显示的最后一行错误无法解决setTexttoString

 public void onButtonClick(View v){}
 EditText e1=(EditText)findViewById(R.id.editText);
 EditText e2=(EditText)findViewById(R.id.editText2);
 TextView t1=(TextView)findViewById(R.id.textView);
 int num1 = Integer.parseInt(e1.getText().toString());
 int num2 = Integer.parseInt(e2.getText().toString());
 int sum = num1 + num2;
 t1.setText(Integer.toString(sum));

2 个答案:

答案 0 :(得分:0)

以下内容应该有效:

   public void onButtonClick(View v){
        EditText e1=(EditText)findViewById(R.id.editText);
        EditText e2=(EditText)findViewById(R.id.editText2);
        TextView t1=(TextView)findViewById(R.id.textView);
        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());
        int sum = num1 + num2;
        t1.setText(Integer.toString(sum));
    }

答案 1 :(得分:0)

这可以改变数据类型

public void onButtonClick(View v){
        EditText e1=(EditText)findViewById(R.id.editText);
        EditText e2=(EditText)findViewById(R.id.editText2);
        TextView t1=(TextView)findViewById(R.id.textView);
        Integer num1 = Integer.parseInt(e1.getText().toString());
        Integer num2 = Integer.parseInt(e2.getText().toString());
        Integer sum = num1 + num2;
        t1.setText(Integer.toString(sum));
    }