Android Button Works Only on the second Click

前端 未结 2 437
甜味超标
甜味超标 2021-01-23 04:51

I am Developing my first android Calculator app. I\'m stuck with a single defect. I have added few buttons and on clicking those buttons, it will put the respectives text on the

相关标签:
2条回答
  • 2021-01-23 05:22

    If you are calling your set1() method onClick of button in xml then you don't need to find ID for button in that method again. So simply it looks like,

    public void set1(View v)
    {
      EditText tv = (EditText) findViewById(R.id.textView1);
      String text=tv.getText().toString();
      tv.setText(text+"1");
    }
    

    Now in your xml for button will be

    android:onClick="set1"
    
    0 讨论(0)
  • 2021-01-23 05:44

    Change your set1() method as follows,

    public void set1(View v)
    {
        EditText tv = (EditText) findViewById(R.id.textView1);
        String text=tv.getText().toString();
        tv.setText(text+"1");
    }
    
    0 讨论(0)
提交回复
热议问题