The Button Control

The basic button class in Android is android.widget.Button. There's not much to this type of button, other than how you use it to handle click events (see Listing 4-9).

Listing 4-9. Handling Click Events on a Button

<Button android:id="@+id/ccbtn1"

android:text="@+string/basicBtnLabel" android:typeface="serif" android:textStyle="bold" android:layout_width="fill_parent"

android:layout_height="wrap_content" />

Button btn = (Button)this.findViewById(R.id.ccbtn1);

btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent intent = getButtonIntent(); intent.setAction("some intent data"); setResult(RESULT_OK, intent); finish();

Listing 4-9 shows how to register for a button-click event. You register for the on-click event by calling the setOnClickListener method with an OnClickListener. In Listing 4-9, an anonymous listener is created on the fly to handle click events for btn. When the button is clicked, the OnClick method of the listener is called.

0 0

Post a comment