Android 控件开发之Button


摘自: http://blog.csdn.net/sun6255028/article/details/6685003


sun6255028的专栏


Button,就是按钮,是android中应用最多的组件之一

   Button的显示效果图:



本文程序的main.xml

  

[cpp]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6. <Button  
  7.     android:id="@+id/btn1"    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/btn1"/>  
  11. <Button  
  12.     android:id="@+id/btn2"  
  13.     android:layout_width="fill_parent"   
  14.     android:layout_height="wrap_content"  
  15.     android:text="@string/btn2"/>     
  16. </LinearLayout>   

本程序的string.xml

[cpp]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="btn1">确认</string>  
  4.     <string name="btn2">取消</string>  
  5.     <string name="app_name">Button</string>  
  6. </resources>  

本程序的java文件

[cpp]  view plain copy
  1. package com.sx.Button;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.Toast;  
  9.   
  10.   
  11. public class ButtonActivity extends Activity  
  12. {     
  13.     private Button btn1 = null;  
  14.     private Button btn2 = null;  
  15.   
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState)   
  19.     {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.           
  23.         btn1 = (Button)findViewById(R.id.btn1);      
  24.         btn1.setOnClickListener(new ButtonClicked());  
  25.           
  26.         btn2 = (Button)findViewById(R.id.btn2);  
  27.         btn2.setOnClickListener(new ButtonClicked());      
  28.     }  
  29.       
  30.     //创建Button监听器  
  31.      class ButtonClicked implements OnClickListener  
  32.     {  
  33.     public void onClick(View v)   
  34.     {  
  35.         switch(v.getId())  
  36.         {  
  37.         case R.id.btn1:  
  38.             //系统输出信息  
  39.             System.out.println("你点击了确定按钮");  
  40.             //使用Toast类输出信息  
  41.             Toast.makeText(ButtonActivity.this"你点击了确定按钮", Toast.LENGTH_LONG).show();  
  42.             break;  
  43.         case R.id.btn2:  
  44.             System.out.println("你点击了取消按钮");  
  45.             Toast.makeText(ButtonActivity.this"你点击了取消按钮", Toast.LENGTH_LONG).show();  
  46.             break;  
  47.         }  
  48.     }  
  49.     }  
  50. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值