在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
答案 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
}
})