Android Button Onclick

后端 未结 9 2036
猫巷女王i
猫巷女王i 2020-11-29 03:41

OK I\'m new to android dev\'s and Java, so I\'m having problems with on click method here\'s my code. I know I\'ve gotta be close, thanks in advance. All I want my button to

9条回答
  •  时光取名叫无心
    2020-11-29 04:17

    You need to make the same method name both in layout XML and java code.

    If you use android:onClick="setLogin" then you need to make a method with the same name, setLogin:

    // Please be noted that you need to add the "View v" parameter
    public void setLogin(View v) {
    
    }
    

    ADVICE:
    Do not mix layout with code by using android:onClick tag in your XML. Instead, move the click method to your class with OnClickListener method like:

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        // TODO Auto-generated method stub
      }
     });
    

    Make a layout just for layout and no more. It will save your precious time when you need to refactoring for Supporting Multiple Screens.

提交回复
热议问题