android学习日记03--常用控件button/imagebutton

常用控件
1、button 按钮
android:layout_width="wrap_content" --自适应
android:layout_height="fill_parent" --充满父控件
@+表示声明 如 android:id="@+id/btn_ok"
@表示引用 如 android:text="@string/hello_world"

 

布局组件等一般设置格式

<布局/组件名称
android:属性="属性类型"
……
/>

如:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10     <!-- 线性布局  --> 
11      <LinearLayout
12      android:layout_width="fill_parent"
13      android:layout_height="wrap_content"
14      android:orientation="horizontal">
15     
16         <TextView
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:text="@string/hello_world" 
20             android:id="@+id/tv"
21             />
22     
23     
24         <Button 
25             android:layout_width="wrap_content"
26             android:layout_height="wrap_content"
27             android:text="@string/btn_ok"
28             android:id="@+id/btn_ok"
29             />
30     
31         <!-- wrap_content:自适应 --> 
32         <Button 
33             android:layout_width="wrap_content" 
34             android:layout_height="wrap_content"
35             android:text="@string/btn_cancle"
36             android:id="@+id/btn_cancle"
37             />
38         
39         <ImageButton 
40             android:layout_width="wrap_content" 
41             android:layout_height="wrap_content"
42             android:background="@drawable/blank"
43             android:id="@+id/btn_img"
44             />
45 
46     </LinearLayout>
47     
48 </RelativeLayout>
View Code

 


定义btn_ok = (Button)findViewById(R.id.btn_ok);
添加监听器 可以implements OnClickListener 重写 OnClick方法
也可以通过内部类实现,btn_ok.setOnClickListincer(new OnclickListner() { @override public void OnClick(View v) { ...}})

2、imagebutton
可添加背景图片,其他同Button
event.getAction()==MotionEvent.ACTION_DOWN 监听按下事件
event.getAction()==MotionEvent.ACTION_UP
getResources().getDrawable 获得资源图片

 

Activity代码:

 1 package com.example.button;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 import android.view.MotionEvent;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.view.View.OnTouchListener;
10 import android.widget.Button;
11 import android.widget.ImageButton;
12 import android.widget.TextView;
13 
14 public class MainActivity extends Activity{
15 
16     private TextView tv;
17     private Button btn_ok,btn_cancle;
18     private ImageButton btn_img;
19     
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         
25         tv = (TextView)findViewById(R.id.tv);
26         
27         btn_ok = (Button)findViewById(R.id.btn_ok);       
28         btn_cancle = (Button)findViewById(R.id.btn_cancle);
29         
30         btn_img = (ImageButton)findViewById(R.id.btn_img);        
31         
32         // 内部类 实现 监听
33         btn_img.setOnTouchListener(new OnTouchListener() {
34             
35             @Override
36             public boolean onTouch(View v, MotionEvent event) {
37                 // TODO Auto-generated method stub
38                 if(event.getAction()==MotionEvent.ACTION_DOWN) {
39                     btn_img.setBackgroundDrawable(getResources().getDrawable(R.drawable.face1));                
40                 }else if(event.getAction()==MotionEvent.ACTION_UP) {
41                     btn_img.setBackgroundDrawable(getResources().getDrawable(R.drawable.blank));                
42                 }
43                 return false;
44             }
45         });
46         
47         // 也可以 通过implements OnClickListener 实现监听
48         btn_ok.setOnClickListener(new OnClickListener() {
49             
50             @Override
51             public void onClick(View v) {
52                 // TODO Auto-generated method stub
53                 if(v == btn_ok) {
54                     tv.setText("触发确定按钮事件");
55                 }
56             }
57         });
58         
59         // 内部类 实现监听器
60         btn_cancle.setOnClickListener(new OnClickListener() {
61             
62             @Override
63             public void onClick(View v) {
64                 // TODO Auto-generated method stub
65                 if(v == btn_cancle) {
66                     tv.setText("触发取消按钮事件");
67                 }
68             }
69         });
70         
71         
72     }
73 
74 
75     @Override
76     public boolean onCreateOptionsMenu(Menu menu) {
77         // Inflate the menu; this adds items to the action bar if it is present.
78         getMenuInflater().inflate(R.menu.main, menu);
79         return true;
80     }
81 
82     
83 }
View Code

 

代码运行效果:

点击‘确定‘按钮,左边textview 显示‘触发确定按钮‘

点击 imagebutton 显示笑脸,放开即还原。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。