0

I found the following solution for detecting which of my buttons was pressed:

public void onClick(View v) {
    switch(v.getId())
    {
    case R.id.bteins:
    Toast t = Toast.makeText(this, "bzeins", Toast.LENGTH_SHORT);
    t.show();
    break;
    case R.id.btzwei:
    // handle button B click;
    break;
    case R.id.btdrei:
        Toast t1 = Toast.makeText(this, "btdfrei", Toast.LENGTH_SHORT);
        t1.show();
    break;
    case R.id.btvier:
    // handle button B click;
    break;
    default:
    throw new RuntimeException("Unknow button ID");
    }
    }

My problem is that when I press bteins or btdrei nothing happens. What´s my mistake?

2
  • You need to post a bit more code here to make it easier to answer your question. The error could be in the code that adds the OnClickListener to the buttons, or the button ids could be misspelled, etc... In general, it is better to do your research first and checking your program with a debugger before posting questions to SO Jul 7, 2013 at 12:37
  • please show your activity code, full, so we can help you in a better way and as fast as possible. Jul 7, 2013 at 13:32

4 Answers 4

1

you can implement onClick for each button like this:

    findViewById(R.id.bteins).setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //do some things!
        }
    });
    findViewById(R.id.btzwei).setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //do some things!
        }
    });
....

or implement OnClickListener in activity and set all buttons click to that:

public class Test extends Activity implements OnClickListener
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        findViewById(R.id.bteins).setOnClickListener(this);
        findViewById(R.id.btzwei).setOnClickListener(this);
        findViewById(R.id.btdrei).setOnClickListener(this);
        findViewById(R.id.btvier).setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
            {
            case R.id.bteins:
                Toast t = Toast.makeText(this, "bzeins", Toast.LENGTH_SHORT);
                t.show();
                break;
            case R.id.btzwei:
                // handle button B click;
                break;
            case R.id.btdrei:
                    Toast t1 = Toast.makeText(this, "btdfrei", Toast.LENGTH_SHORT);
                    t1.show();
                break;
            case R.id.btvier:
                // handle button B click;
                break;
            default:
                throw new RuntimeException("Unknow button ID");
            }
    }
}
0

Have you set OnClickListener to the button? I guess you haven't.

0
public class theexample extends Activity implements OnClickListener

//OnCreateEvent
bteins.setOnClickListener(this);
btvier.setOnClickListener(this);

public void OnClick(View v){
switch(v.getId()){
            case R.id.bteins:
                Toast.makeText(this, "bzeins", Toast.LENGTH_SHORT).show();
                t.show();
                break;
            case R.id.btzwei:
                // handle button B click;
                break;
            case R.id.btdrei:
                   Toast.makeText(this, "btdfrei", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btvier:
                // handle button B click;
                break;
            default:
                throw new RuntimeException("Unknow button ID");
            }
    }
0

You need to connect triggers to their respective handlers with setOnClickListener()

This method either gets this as a parameter, and than your activity has to implement OnClickListener (Eclipse can help you auto refactor these things easily.)

Or, just pass it a new OnClickListener on the spot:

Button myButton = (Button) findViewById(*buttons id*);
myButton.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        //You can check v attributes to see what was clicked as well. 
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.