The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 21 | import android.view.accessibility.AccessibilityEvent; |
| 22 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.widget.RemoteViews.RemoteView; |
| 24 | |
| 25 | |
| 26 | /** |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 27 | * Represents a push-button widget. Push-buttons can be |
| 28 | * pressed, or clicked, by the user to perform an action. |
| 29 | |
| 30 | * <p>A typical use of a push-button in an activity would be the following: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | * </p> |
| 32 | * |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 33 | * <pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | * public class MyActivity extends Activity { |
| 35 | * protected void onCreate(Bundle icicle) { |
| 36 | * super.onCreate(icicle); |
| 37 | * |
| 38 | * setContentView(R.layout.content_layout_id); |
| 39 | * |
| 40 | * final Button button = (Button) findViewById(R.id.button_id); |
| 41 | * button.setOnClickListener(new View.OnClickListener() { |
| 42 | * public void onClick(View v) { |
| 43 | * // Perform action on click |
| 44 | * } |
| 45 | * }); |
| 46 | * } |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 47 | * }</pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | * |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 49 | * <p>However, instead of applying an {@link android.view.View.OnClickListener OnClickListener} to |
| 50 | * the button in your activity, you can assign a method to your button in the XML layout, |
| 51 | * using the {@link android.R.attr#onClick android:onClick} attribute. For example:</p> |
| 52 | * |
| 53 | * <pre> |
| 54 | * <Button |
| 55 | * android:layout_height="wrap_content" |
| 56 | * android:layout_width="wrap_content" |
| 57 | * android:text="@string/self_destruct" |
| 58 | * android:onClick="selfDestruct" /></pre> |
| 59 | * |
| 60 | * <p>Now, when a user clicks the button, the Android system calls the activity's {@code |
| 61 | * selfDestruct(View)} method. In order for this to work, the method must be public and accept |
| 62 | * a {@link android.view.View} as its only parameter. For example:</p> |
| 63 | * |
| 64 | * <pre> |
| 65 | * public void selfDestruct(View view) { |
| 66 | * // Kabloey |
| 67 | * }</pre> |
| 68 | * |
| 69 | * <p>The {@link android.view.View} passed into the method is a reference to the widget |
| 70 | * that was clicked.</p> |
| 71 | * |
| 72 | * <h3>Button style</h3> |
| 73 | * |
| 74 | * <p>Every Button is styled using the system's default button background, which is often different |
| 75 | * from one device to another and from one version of the platform to another. If you're not |
| 76 | * satisfied with the default button style and want to customize it to match the design of your |
| 77 | * application, then you can replace the button's background image with a <a |
| 78 | * href="{@docRoot}guide/topics/resources/drawable-resource.html#StateList">state list drawable</a>. |
| 79 | * A state list drawable is a drawable resource defined in XML that changes its image based on |
| 80 | * the current state of the button. Once you've defined a state list drawable in XML, you can apply |
| 81 | * it to your Button with the {@link android.R.attr#background android:background} |
| 82 | * attribute. For more information and an example, see <a |
| 83 | * href="{@docRoot}guide/topics/resources/drawable-resource.html#StateList">State List |
| 84 | * Drawable</a>.</p> |
| 85 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 86 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/button.html">Buttons</a> |
| 87 | * guide.</p> |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 88 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | * <p><strong>XML attributes</strong></p> |
Scott Main | 6ec3d4f | 2010-09-22 18:55:31 -0700 | [diff] [blame] | 90 | * <p> |
| 91 | * See {@link android.R.styleable#Button Button Attributes}, |
| 92 | * {@link android.R.styleable#TextView TextView Attributes}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | * {@link android.R.styleable#View View Attributes} |
| 94 | * </p> |
| 95 | */ |
| 96 | @RemoteView |
| 97 | public class Button extends TextView { |
| 98 | public Button(Context context) { |
| 99 | this(context, null); |
| 100 | } |
| 101 | |
| 102 | public Button(Context context, AttributeSet attrs) { |
| 103 | this(context, attrs, com.android.internal.R.attr.buttonStyle); |
| 104 | } |
| 105 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 106 | public Button(Context context, AttributeSet attrs, int defStyleAttr) { |
| 107 | this(context, attrs, defStyleAttr, 0); |
| 108 | } |
| 109 | |
| 110 | public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 111 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 113 | |
| 114 | @Override |
| 115 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 116 | super.onInitializeAccessibilityEvent(event); |
| 117 | event.setClassName(Button.class.getName()); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 122 | super.onInitializeAccessibilityNodeInfo(info); |
| 123 | info.setClassName(Button.class.getName()); |
| 124 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } |