Android Button Onclick

后端 未结 9 2035
猫巷女王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:20

    Here is some sample code of how to add a button named Add. You should declare the variable as a member variable, and the naming convention for member variables is to start with the letter "m".

    Hit Alt+Enter on the classes to add the missing references.

    Add this to your activity_main.xml:

     

    Add this to your MainActivity.java:

    public class MainActivity extends AppCompatActivity {
    
        Button mButtonAdd; 
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mButtonAdd = findViewById(R.id.buttonAdd);
    
            mButtonAdd.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // do something here
                }
            });
        }
    }
    

提交回复
热议问题