Android 基本UI(二)—Button、ImageButton
来源:互联网 发布:淘宝达人怎么赚钱的 编辑:程序博客网 时间:2024/05/19 16:03
Android系统的界面控件分为定制控件和系统控件,系统控件是Android系统提供给用户已经封装的界面控件,是在应用程序开发过程中常见功能控件。在开发应用中,经常使用的系统控件有TextView、EditText、Button、ImageButton、Checkbox、RadioButton、Spinner、ListView和TabHost等。
(一)按钮Button
Button是一种常用的按钮控件,继承自android.widget.TextView,在android.widget包中。
Button按钮控件的通常用法是
在程序中通过super.findViewById(id)得到在layout中XML文件中声明的Button的引用,然后使用setOnClickListener(View.OnClickListener)添加监听,再在View.OnClickListener监听器中使用v.equals(View)方法判断是哪一个按钮被按下,调用不同方法进行分别处理。
1)用xml描述一个button
<Button Android:id="@+id/button"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="这是一个button" />
2)在程序代码中引用用xml描述的button:
Button button = (Button) findViewById(R.id.button);
3)给Button设置事件响应
button.setOnClickListener(button_listener);
4)生成一个按钮事件监听器
private Button.OnClickListener button_listener = new
Button.OnClickListener() {
public void onClick(View v) {
switch(v.getId()){
case R.id.Button:
textView.setText("Button按钮1");
return;
case R.id.Button01:
textView.setText("Button按钮2");
return;
}
}
};
此外,也可以采用在layout中XML文件中声明分配一个方法给Button按钮,使用android:onClick属性
当用户点击Button按钮时,Android系统会自动调用activity中的方法
(二)图片按钮ImageButton :
ImageButton继承自ImageView类,是用以实现能够显示图像功能的控件按钮,既可以显示图片又可以作为Button使用。ImageButton与Button之间的区别:ImageButton中没有text属性。ImageButton控件中设置按钮中显示的图片可以通过android:src属性来设置。也可以通过setImageResource(int)来设置
使用XML描述ImageButton控件,并在程序中引用和使用的简要过程:
1)在res/layout文件下的XML文件中描述ImageButton控件。
<ImageButton android:id="@+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
2)在程序中引用XML描述的ImageButton
ImageButton imageButton = (ImageButton)findViewById(R.id.ImageButton01);
3)利用setImageResource()函数,将新加入的png文件R.drawable.download传递给ImageButton
imageButton.setImageResource(R.drawable.download);
- Android 基本UI(二)—Button、ImageButton
- android整合--UI基本控件Button,ImageButton,EditText,ChcekBox,ToggleButton,RadioButton
- Android基本控件之Button和ImageButton
- Android基本控件 - Button和ImageButton
- Android学习笔记---第三天---基础UI组件---Button&ImageButton
- Android学习-常见的UI控件 Button和ImageButton
- Android学习(三)Button和imageButton
- android Button ImageButton 区别
- android Button ImageButton 区别
- android Button ImageButton 区别
- Android之Button,ImageButton
- Android-Button和ImageButton
- Android-Button和ImageButton
- Android第三课;Button与ImageButton的基本属性
- android中的布局(二)Button 和ImageButton的简单用法
- 02 - 基本控件:按钮控件(Button、ImageButton、ToggleButton)
- Android自定义Button/ImageButton形状
- android笔记3-button,imageButton
- 浅谈程序员的薪资和工作经验
- REST的php初探
- Mysql Replication Troubleshooting - Classic VS GTID
- 我用USB转串口用CRT,Xshell,连接后都无法输入
- O_NONBLOCK与O_NDELAY有何不同
- Android 基本UI(二)—Button、ImageButton
- 如何实现 对输入框的文本内容进行限制的功能
- category 给IOS原生类添加属性
- java I/O流学习(四)
- 2014-12-8课堂笔记:转换场景的实现
- unity3d 纹理的精灵动画效果
- MS SQL SERVER 2008不能用IP登录问题的解决方案
- ttf字库瘦身,只保留自己想要的字
- iOS的缓存机制