简体   繁体   中英

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.

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.

Overriding the onClick() is right and your MainActivity implements the OnClickListener correctly.

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.

Example :

btnCheck = (Button) findViewById(R.id.btnCheck);
btnCheck.setOnClickListener(this);

Add following lines in onCreate method at last.

Create Button object uning findViewbyId and setOnClickListener to that object

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 .

    @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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM