how to add button click event in android studio

前端 未结 14 1329
面向向阳花
面向向阳花 2020-12-24 11:04

So I have done some research, and after defining you button as an object by the code

private Button buttonname;
buttonname = (Button) findViewById(R.id.butto         


        
14条回答
  •  甜味超标
    2020-12-24 11:08

    You need to do nothing but, just type new View.OnClickListener() in your btnClick.setOnClickListener(). I mean you have to type btnClick.setOnClickListener(new View.onClickListener the bracket will remain open, just hit enter then a method will be created like this,

    btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
            }
        });
    

    It's just mean that you are creating a new view of OnClickListener and passing a view as it's a parameter. Now you can do whatever u were supposed to do, in that onClick(View v) method.

提交回复
热议问题