开发者

onClick handler android - problem specific question

I'm developing a sample app for android and my level is beginner.

For simplicity assume that I have three buttons namely A,B,C.

Instead of these buttons triggering different functions every time as they are clicked, all these buttons trigger the same method "onClick" which is defined in XML.

How can I开发者_如何学JAVA get the name of the button that triggered onClick method, in what way should I use the view class ?

Thanks in advance for your time.


Take a look at this. Should do exactly what you want, with some suggestions for other implementations as well :)


Another Way.

public void onClick(View v) {
    // Perform action on click
    String msg;
    int id = v.getId();

    Button inB = (Button) v.findViewById(id);

    msg = "Clicked Button ID From Object " + inB.getId() + " Button";
    Toast.makeText(v.getContext(), msg, Toast.LENGTH_LONG).show();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜