Android入门:Button
一、Button的继承结构
java.lang.Object
|-android.view.View
|-android.widget.TextView
|-android:widget.Button
二、Button使用
1.监听器
android.view.View.OnClickListener
btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //此处的View为Button,可强制类型转换 } });
实例:
package org.xiazdong; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn; int i=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button)this.findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Button b = (Button)v; b.setText(""+(i++)); } }); } }