标签:blog xmlns wrapper one demo int length actor html
Button继承了TextView。它的功能就是提供一个button,这个button能够供用户点击。当用户对button进行操作的时候,触发对应事件,如点击。触摸。一般对于一个button而言,用的最多的就是点击事件,Button间接继承自View。而Android UI中的全部事件。都是定义在View中的。
实例:ButtonDemo
执行效果:
代码清单:
布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button2" /> </LinearLayout>
package com.rainsong.buttondemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ActivityButton extends Activity { Button btn1; Button btn2; OnClickListener listener1; OnClickListener listener2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listener1 = new OnClickListener() { public void onClick(View v) { Toast.makeText(ActivityButton.this, "Button1 clicked",Toast.LENGTH_SHORT).show(); } }; listener2 = new OnClickListener() { public void onClick(View v) { Toast.makeText(ActivityButton.this, "Button2 clicked",Toast.LENGTH_SHORT).show(); } }; btn1 = (Button)findViewById(R.id.button1); btn1.setOnClickListener(listener1); btn2 = (Button)findViewById(R.id.button2); btn2.setOnClickListener(listener2); } }
标签:blog xmlns wrapper one demo int length actor html
原文地址:http://www.cnblogs.com/gccbuaa/p/6774287.html