完整内容,请查看:http://www.zhdoc.net/android/reference/android/widget/Button.html
Button
public class Button
extends TextView
java.lang.Object | |||
↳ | android.view.View | ||
↳ | android.widget.TextView | ||
↳ | android.widget.Button |
Represents a push-button widget. Push-buttons can be pressed, or clicked, by the user to perform an action.
按钮控件。按钮由用户执行一个操作,它可以被按下,或点击。
A typical use of a push-button in an activity would be the following:
在activity中使用按钮的通常用法如下:
public class MyActivity extends Activity { protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.content_layout_id); final Button button = (Button) findViewById(R.id.button_id); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); } }
However, instead of applying an OnClickListener
to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick
attribute. For example:
但是,你可以在XML布局中使用android:onClick
属性,将一个方法赋值给按钮,而不是用OnClickListener
来响应你activity中的按钮的操作。例如:
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/self_destruct" android:onClick="selfDestruct" />
Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View)
method. In order for this to work, the method must be public and accept a View
as its only parameter. For example:
现在,当用户点击按钮时,Android系统将调用activity的selfDestruct(View)
的方法。为使其能够工作,这个方法必须是public的,并且仅接受一个View
类型的参数。例如:
public void selfDestruct(View view) { // Kabloey }
The View
passed into the method is a reference to the widget that was clicked.
传入到此方法的View
参数,将是被点击的控件的对象。
按钮样式
Every Button is styled using the system's default button background, which is often different from one device to another and from one version of the platform to another. If you're not satisfied with the default button style and want to customize it to match the design of your application, then you can replace the button's background image with a state list drawable. A state list drawable is a drawable resource defined in XML that changes its image based on the current state of the button. Once you've defined a state list drawable in XML, you can apply it to your Button with the android:background
attribute. For more information and an example, see State List Drawable.
每个按钮都是使用系统默认的按钮背景,因此不同的设备上的不同平台版本,将出现不同的效果。如果你对默认的按钮样式不满意,并且想要定制它来匹配你的应用程序的设计,那么你可以使用一个状态列表drawable来替换按钮的背景图像。状态列表drawable是定义在XML中的一个drawable的资源,其根据按钮的当前状态来更改图像。一旦你已经在XML中定义了一个状态列表drawable,则你可以将它赋值给按钮的android:background
属性。更多的信息和示例,请参考状态列表drawable。
See the Buttons guide.
请参考Buttons。
XML attributes
XML属性
完整内容,请查看:http://www.zhdoc.net/android/reference/android/widget/Button.html