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; 21 import android.widget.RemoteViews.RemoteView; 22 23 24 /** 25 * Represents a push-button widget. Push-buttons can be 26 * pressed, or clicked, by the user to perform an action. 27 28 * <p>A typical use of a push-button in an activity would be the following: 29 * </p> 30 * 31 * <pre> 32 * public class MyActivity extends Activity { 33 * protected void onCreate(Bundle icicle) { 34 * super.onCreate(icicle); 35 * 36 * setContentView(R.layout.content_layout_id); 37 * 38 * final Button button = (Button) findViewById(R.id.button_id); 39 * button.setOnClickListener(new View.OnClickListener() { 40 * public void onClick(View v) { 41 * // Perform action on click 42 * } 43 * }); 44 * } 45 * }</pre> 46 * 47 * <p>However, instead of applying an {@link android.view.View.OnClickListener OnClickListener} to 48 * the button in your activity, you can assign a method to your button in the XML layout, 49 * using the {@link android.R.attr#onClick android:onClick} attribute. For example:</p> 50 * 51 * <pre> 52 * <Button 53 * android:layout_height="wrap_content" 54 * android:layout_width="wrap_content" 55 * android:text="@string/self_destruct" 56 * android:onClick="selfDestruct" /></pre> 57 * 58 * <p>Now, when a user clicks the button, the Android system calls the activity's {@code 59 * selfDestruct(View)} method. In order for this to work, the method must be public and accept 60 * a {@link android.view.View} as its only parameter. For example:</p> 61 * 62 * <pre> 63 * public void selfDestruct(View view) { 64 * // Kabloey 65 * }</pre> 66 * 67 * <p>The {@link android.view.View} passed into the method is a reference to the widget 68 * that was clicked.</p> 69 * 70 * <h3>Button style</h3> 71 * 72 * <p>Every Button is styled using the system's default button background, which is often different 73 * from one device to another and from one version of the platform to another. If you're not 74 * satisfied with the default button style and want to customize it to match the design of your 75 * application, then you can replace the button's background image with a <a 76 * href="{@docRoot}guide/topics/resources/drawable-resource.html#StateList">state list drawable</a>. 77 * A state list drawable is a drawable resource defined in XML that changes its image based on 78 * the current state of the button. Once you've defined a state list drawable in XML, you can apply 79 * it to your Button with the {@link android.R.attr#background android:background} 80 * attribute. For more information and an example, see <a 81 * href="{@docRoot}guide/topics/resources/drawable-resource.html#StateList">State List 82 * Drawable</a>.</p> 83 * 84 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/button.html">Buttons</a> 85 * guide.</p> 86 * 87 * <p><strong>XML attributes</strong></p> 88 * <p> 89 * See {@link android.R.styleable#Button Button Attributes}, 90 * {@link android.R.styleable#TextView TextView Attributes}, 91 * {@link android.R.styleable#View View Attributes} 92 * </p> 93 */ 94 @RemoteView 95 public class Button extends TextView { 96 public Button(Context context) { 97 this(context, null); 98 } 99 100 public Button(Context context, AttributeSet attrs) { 101 this(context, attrs, com.android.internal.R.attr.buttonStyle); 102 } 103 104 public Button(Context context, AttributeSet attrs, int defStyleAttr) { 105 this(context, attrs, defStyleAttr, 0); 106 } 107 108 public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 109 super(context, attrs, defStyleAttr, defStyleRes); 110 } 111 112 @Override 113 public CharSequence getAccessibilityClassName() { 114 return Button.class.getName(); 115 } 116 } 117