Android 笔记 之 Button
2010-12-30 09:46
190 查看
Button,毋庸置疑,按钮是也。
下面介绍几种给按钮添加监听事件的方法。
1.XML文件。
2.
下面介绍几种给按钮添加监听事件的方法。
1.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" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="按钮" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1_OnClick" android:onClick="button1Clicked" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退出" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮4" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮5" /> </LinearLayout>
2.
import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.os.Bundle; import android.widget.Button; import android.widget.Toast; public class ButtonDemo extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //从资源文件中获取按钮对象的引用 Button sButton2 = (Button)findViewById(R.id.button2); Button sButton3 = (Button)findViewById(R.id.button3); //注册点击事件监听者 sButton2.setOnClickListener(this); sButton3.setOnClickListener(this); Button sButton4 = (Button)findViewById(R.id.button4); sButton4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(ButtonDemo.this, "button4 is clicked", Toast.LENGTH_LONG).show(); } }); Button sButton5 = (Button)findViewById(R.id.button5); sButton5.setOnClickListener(button5ClickedListener); } /** * 按钮响应事件 */ public void onClick(View aView) { switch (aView.getId()) { case R.id.button2: setTitle("button2 is clicked"); break; case R.id.button3: finish(); break; } } public void button1Clicked(View view){ Toast.makeText(ButtonDemo.this, "button1 is clicked", Toast.LENGTH_LONG).show(); } View.OnClickListener button5ClickedListener = new View.OnClickListener() { @Override public void onClick(View arg0) { new View.OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(ButtonDemo.this, "button5 is clicked", Toast.LENGTH_LONG).show(); } } } }; }
相关文章推荐
- Android开发笔记:Button的基本用法
- android笔记3-button,imageButton
- 初学Android,笔记1(view的引用和Button监听器的语法)
- Android入门笔记 - 界面开发 - TextView,Button,EditText,Toast
- Android笔记:Button居中|水平居中|垂直居中(总结)
- Android应用开发笔记(10):制作自定义背景Button按钮、自定义形状Button的全攻略
- Android 应用开发笔记 - 按钮(Button, ImageButton)
- android 用户界面笔记之Button和ImageButton实现按钮响应事件两种方法
- android笔记 自定义textview,button等控件的外观,点击效果
- Android笔记:Button居中|水平居中|垂直居中
- android开发中ListView中存在Button时ListView无法获取焦点(笔记)
- Android-学习笔记之 Button实现点击事件的几种方式
- Android开发学习笔记:浅谈ToggleButton
- Android开发学习笔记:圆角的Button
- Android笔记之Button|Textiew设置监听
- Android开发学习笔记:圆角的Button
- Android笔记:实现圆角的Button
- Android开发学习笔记:圆角的Button
- Android自学笔记之Button按钮里的属性及用法
- Android 开发笔记 1.2 Button