how to add button click event in android studio

前端 未结 14 1357
面向向阳花
面向向阳花 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:23

    SetOnClickListener (Android.View.view.OnClickListener) in View cannot be applied to (com.helloandroidstudio.MainActivity)

    This means in other words (due to your current scenario) that your MainActivity need to implement OnClickListener:

    public class Main extends ActionBarActivity implements View.OnClickListener {
       // do your stuff
    }
    

    This:

    buttonname.setOnClickListener(this);
    

    means that you want to assign listener for your Button "on this instance" -> this instance represents OnClickListener and for this reason your class have to implement that interface.

    It's similar with anonymous listener class (that you can also use):

    buttonname.setOnClickListener(new View.OnClickListener() {
    
       @Override
       public void onClick(View view) {
    
       }
    });
    

提交回复
热议问题