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