有关setOnclicklistener的活动出错

时间:2016-10-12 12:17:38

标签: android

在Android活动setonclicklistener方法中,我们可以使用this作为参数吗?我收到了一个错误。我犯了什么错误吗?

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_login); //Loading XML in to empty Window
tvName = (TextView) findViewById(R.id.tvName);
btnSubmit = (Button)findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(this);//error

3 个答案:

答案 0 :(得分:1)

如下所示实现侦听器:

    public class youClassName extends AppCompatActivity implements View.OnClickListener
        {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);
        tvName = (TextView) findViewById(R.id.tvName); 
        btnSubmit = (Button) findViewById(R.id.btnSubmit); 
         btnSubmit.setOnClickListener(this);

          } 
        }

答案 1 :(得分:0)

试试这个

  btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             // your onClick code goes here!
        }
    });

答案 2 :(得分:0)

要实现OnClickListener,您基本上可以这样做:

    public class YourActivity extends Activity implements View.OnClickListener {
   // Using this way you must implement OnClickListener interface ^
   // someComponents.setOnClickListener(this);

   @Override
   public void onClick (View v) {
     switch(v.getId()) {
       case R.id.your_component_id:
         // do your job here  
         break;
    }
   } 

}

yourComponent.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      // do your job here
    }

})