用户界面View之Switch与ToggleButton
来源:互联网 发布:cgss卡牌数据库 编辑:程序博客网 时间:2024/05/21 19:28
在我们了解什么是生命之前,我们已将它消磨了一半。
本讲内容:Switch 开关控件和ToggleButton控件
示例一:
下面是res/layout/activity_main.xml 布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="WIFI设置" android:textSize="20sp" /> <Switch android:id="@+id/open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:textOn="WIFI开启中" android:textOff="WIFI关闭中"/></RelativeLayout>
下面是MainActivity主界面文件:
public class MainActivity extends Activity {private Switch open;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);open=(Switch) findViewById(R.id.open);open.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(isChecked){Toast.makeText(MainActivity.this, "打开WIFI", Toast.LENGTH_LONG).show();}else{Toast.makeText(MainActivity.this, "关闭WIFI", Toast.LENGTH_LONG).show();}}});}}
示例二:
下面是res/layout/activity_main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- textOn:true textOff:false 默认是false--> <ToggleButton android:id="@+id/id_toggleButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:textOff="关" android:textOn="开" android:checked="false"/> <ImageView android:id="@+id/id_imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/off"/></LinearLayout>
下面是MainActivity主界面文件:
public class MainActivity extends Activity implements OnCheckedChangeListener{private ToggleButton tb;private ImageView img;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);tb=(ToggleButton) findViewById(R.id.id_toggleButton);img=(ImageView) findViewById(R.id.id_imageView);tb.setOnCheckedChangeListener(this);}public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {/* *当tb被点击的时候,当前的方法会执行 * buttonView 代表被点击控件的本身 * isChecked 代表被点击的控件的状态 */img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);}}
0 0
- 用户界面View之Switch与ToggleButton
- Switch与ToggleButton
- UI控件之开关按钮(ToggleButton与Switch)
- Android之RadioButton与RadioGroup,CheckBox,Switch,ToggleButton的介绍
- Android控制之ToggleButton和Switch与RatingBar
- 开关按钮ToggleButton与Switch
- Android控件之ToggleButton、Switch
- widgets之switch和ToggleButton
- Android用户界面基础之ToggleButton学习
- 用户界面View之DatePicker与TimePicker
- 用户界面View之ListView与Adapter(一)
- 用户界面View之Button与ImageButton控件
- Android基础控件 - ToggleButton与Switch
- Android UI控件之ToggleButton、Switch
- CompoundButton之RadioButton、CheckBox、ToggleButton、Switch
- Android学习之RatingBar,SeekBar,ToggleButton,Switch
- 用户界面View之CheckBox
- 用户界面View之RadioButton
- 提高Oracle查询效率
- 妙龄女台北地铁裸拍 公然露乳露下体(图)
- 人脸识别之特征脸方法(Eigenface)
- ubuntu system setting no everthing
- linux下自动启动mysql
- 用户界面View之Switch与ToggleButton
- 深入浅出学习 OpenStack
- [DX11调试]检查D3D对象是否释放:ReportLiveObjects()的用法
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(2015浙江省赛---第三部分)
- Linux中断处理驱动程序编写
- 安装sqlserver2008的教训
- 面试题1
- 安卓学习资料——读《疯狂Android讲义》有感
- Android学习 - android.intent.action.MAIN与android.intent.category.LAUNCHER理解