02 - 基本控件:按钮控件(Button、ImageButton、ToggleButton)
来源:互联网 发布:集体智慧编程 编辑:程序博客网 时间:2024/05/20 06:31
转载说明出处:http://blog.csdn.net/eana_don/article/details/8246770
参考学习资料:《疯狂Android讲义》(部分代码与此书源代码类似)
普通按钮和图片按钮。点击之后响应点击操作。按钮还很经常有一种格式,当它正常时是一种样式,被点击后就会更换背景。
XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一般Button" /> <!-- ImageButton不可以添加文字,就算指定了text属性,也不会出现 ,当设置完src之后也不可以改变图片资源 --> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login05" android:contentDescription="imageButton" /> <!-- 设置背景色为透明是为了去除Android的按钮自带背景样式 --> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login05" android:contentDescription="imageButton" android:background="#00000000" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一般Button左置图片" android:drawableLeft="@drawable/ic_action_search" /> <!-- 状态按钮需要制作一个选择器,这个选择器其实是一个XML文件 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="状态按钮" android:background="@drawable/btn_state_bg" /> </LinearLayout>状态按钮对应的xml文件btn_state_bg.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 被点击时候的样式 --> <item android:state_pressed="true" android:drawable="@drawable/login_button_pressed"/> <!-- 无点击时候的样式 --> <item android:state_pressed="false" android:drawable="@drawable/login_button_normal"/></selector>
效果图:正常无点击时 状态按钮被点击时
按钮还有最重要的操作就是点击的监听。如果没有监听,这个按钮就没有起作用。按钮的监听步骤为:
1.首先获取这个按钮
2.为按钮添加监听。
如下:
login = (Button)findViewById(R.id.Button01); login.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { //do something } });ToggleButton是一种特殊的按钮,适用于改变状态。按钮就像开关一样有两种状态。当点击后处于“开”状态,当再次点击后处于“关”状态。用法也很简单。
xml写法:
<!-- textOn:设置按钮被选中时的文字 textOff:设置按钮没有被选中时的文字--> <ToggleButton android:textOn="ON" android:textOff="OFF" android:id="@+id/tbtn_toggle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checked="true"/>
代码中获取状态改变:setContentView(R.layout.activity_toggle); tbtn = (ToggleButton)findViewById(R.id.tbtn_toggle); lin = (LinearLayout)findViewById(R.id.ll); tbtn.setOnCheckedChangeListener( new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){lin.setOrientation(1);//垂直} else{lin.setOrientation(0);}}} );效果如下:
- 02 - 基本控件:按钮控件(Button、ImageButton、ToggleButton)
- 按钮控件(Button,LinkButton,ImageButton)
- android整合--UI基本控件Button,ImageButton,EditText,ChcekBox,ToggleButton,RadioButton
- android 学习 ---控件按钮,Button 与ImageButton,
- Android基本控件之Button和ImageButton
- Android基本控件 - Button和ImageButton
- Button和ImageButton控件
- Android基本控件:button按钮
- Android 按钮类控件大集锦:Button ToggleButton CheckBox RadioButton
- UI控件之Button(按钮)和ImageButton(图像按钮)
- Android中的基本控件(上)--按钮控件Button
- Android-基本控件(AutoCompleteTextView,MultiAutoCompleteTextView,ToggleButton)
- MFC控件之Button基本用法 (动、静态按钮)
- MFC按钮控件(Button)
- Qt按钮控件(Button)
- 按钮控件Button(文本类控件)
- Windows button控件(按钮控件)
- ASP:ImageButton控件(按钮类型控件)
- SO_KEEPALIVE
- HELLOWIN(2)
- Texture Wrapping Mode
- 英语练习147 What's new
- MyEclipse 6.5 启动后出现“initializing java tooling(0%)” 后程序未响应解决
- 02 - 基本控件:按钮控件(Button、ImageButton、ToggleButton)
- MyEclipse 6.5启动在“Loading com.genuitec.eclipse.sqlexplorer”卡住解决
- mysql 的load data local infile命令
- android学习笔记之BroadcastReceiver应用详解
- 程序员面试题精选100题(55)-不用+、-、×、÷做加法
- MySQL修改字段默认值
- 读《AskForHelp——提问的智慧》感想
- POJ 3126解题报告
- MySQL修改字段允许为空