开发者

event handler in android - designer time automation and handling 2 views in separate handlers

Im new to the eclipse(Indigo) and android and I do come from the Visual Studio 2010 and C#.

  1. In the VS when i double clicking an event in the designer (like the click of the Button class) the VS2010 create by itself the handler method and register it to the event. how can I do it in the eclipse? the link here show what i want as a result but its not not the way

  2. lets say I have 2 buttons, how do 开发者_运维技巧i set to different event handler for each one of them, and not in the anonymous way, and if possible, not using the switch on the same handler to decide which view fired the callback?


The Android Development Tools for Eclipse do not have the functionality you mention in your first question - you have to implement the code manually.

Grab the button from the view in your activity (onCreate) and attach a listener to it. To answer your second question as well, the code is:

button1= (Button) findViewById(R.id.button1);
button1.setOnClickListener(new Button1Listener());

Button1Listener is a class for specifically handling the click to someButton - you can ignore the view given to you in onClick.

class Button1Listener implements OnClickListener {

    @Override
    public void onClick(View v) {
        // do stuff
    }
}

The button XML is:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜