[英]How to implemente OnClickListener in an activity?
In my application I have a simple activity with 3 buttons, and to not set an individual OnClickListener for each button, I decided to implement it in my activity, but it doesn't work. 在我的应用程序中,我有一个带有3个按钮的简单活动,并且为了不为每个按钮设置单独的OnClickListener,我决定在我的活动中实现它,但是它不起作用。
Here is my code 这是我的代码
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void CheckForViolations(){
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnCheck:
CheckForViolations();
break;
case R.id.btnViewAllViolations:
Intent violationListIntent = new Intent(MainActivity.this,ViolationListActivity.class);
startActivity(violationListIntent);
break;
case R.id.btnSettings:
Intent settingsIntent = new Intent(MainActivity.this, ViolationListActivity.class);
startActivity(settingsIntent);
break;
}
}
}
Am i missing something, because the program does not even stop in onClick method when i debug my app. 我是否缺少某些东西,因为在调试我的应用程序时,该程序甚至没有在onClick方法中停止。
Overriding the onClick() is right and your MainActivity implements the OnClickListener correctly. 覆盖onClick()是正确的,并且您的MainActivity可以正确实现OnClickListener。
You'll just need to register each of the buttons. 您只需要注册每个按钮。 For example: 例如:
Button goButton = (Button) findViewById(R.id.buttonGo);
goButton.setOnClickListener(this);
you have to set view's listener as activity. 您必须将视图的侦听器设置为活动。 so, it should be like this: 因此,应该是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View btnCheck = findViewById(R.id.btnCheck);
btnCheck.setOnClickListener(this);
View btnViewAllViolations = findViewById(R.id.btnViewAllViolations);
btnViewAllViolations.setOnClickListener(this);
View btnSettings = findViewById(R.id.btnSettings);
btnSettings.setOnClickListener(this);
}
You need to findViewbyId
your buttons and setOnClickListener
for them. 您需要findViewbyId
您的按钮和setOnClickListener
他们。
Example : 范例:
btnCheck = (Button) findViewById(R.id.btnCheck);
btnCheck.setOnClickListener(this);
Add following lines in onCreate
method at last. 最后在onCreate
方法中添加以下行。
Create Button
object uning findViewbyId
and setOnClickListener
to that object 创建Button
对象,将findViewbyId
和setOnClickListener
到该对象
Button b1 = (Button) findViewById(R.id.btnCheck);
Button b2 = (Button) findViewById(R.id.btnViewAllViolations);
Button b3 = (Button) findViewById(R.id.btnSettings);
b1 .setOnClickListener(this);
b2 .setOnClickListener(this);
b3 .setOnClickListener(this);
You need to declare Button
to setOnClickListener
. 您需要将Button
声明为setOnClickListener
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCheck=(Button)findViewById(R.id.btnCheck1);
Button btnViewAllViolations=(Button)findViewById(R.id.btnViewAllViolations1);
Button btnSettings=(Button)findViewById(R.id.btnSettings1);
//Where btnCheck1,btnViewAllViolations1,btnSettings1 are ids in xml;
btnCheck.setOnClickListener(this);
btnViewAllViolations.setOnClickListener(this);
btnSettings.setOnClickListener(this);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.