Android学习路之开关按钮ToggleButton和开关Switch
2017-06-18 12:57
477 查看
开关按钮ToggleButton和开关Switch
实现代码:先是两个drawable的文件:thumb_selctor.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/switch_btn_pressed"/> <item android:state_pressed="false" android:drawable="@drawable/switch_btn_normal"/> </selector>
track_selctor.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/switch_btn_bg_green"/> <item android:state_checked="false" android:drawable="@drawable/switch_btn_bg_white"/> </selector>
布局文件: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" tools:context=".MainActivity"> <ToggleButton android:id="@+id/tbtn_open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:textOff="关闭声音" android:textOn="打开声音" /> <Switch android:id="@+id/swh_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="" android:textOn="" android:thumb="@drawable/thumb_selctor" android:track="@drawable/track_selctor" /> </LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ private ToggleButton tbtn_open; private Switch swh_status; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tbtn_open = (ToggleButton) findViewById(R.id.tbtn_open); swh_status = (Switch) findViewById(R.id.swh_status); tbtn_open.setOnCheckedChangeListener(this); swh_status.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { switch (compoundButton.getId()){ case R.id.tbtn_open: if(compoundButton.isChecked()) Toast.makeText(this,"打开声音",Toast.LENGTH_SHORT).show(); else Toast.makeText(this,"打开声音",Toast.LENGTH_SHORT).show(); break; case R.id.swh_status: if(compoundButton.isChecked()) Toast.makeText(this,"开关:ON",Toast.LENGTH_SHORT).show(); else Toast.makeText(this,"开关:OFF",Toast.LENGTH_SHORT).show(); break; } } }
运行效果:
相关文章推荐
- Android入门教程二十八之开关按钮ToggleButton和开关Switch
- android-UI组件实例大全(五)------开关按钮ToggleButton和开关Switch
- Android控件之ProgressBar(进度条),ToggleButton/SwitchBar(开关按钮),SeekBar(拖动条)/RatingBar(等级评分)
- Android基础入门教程——2.3.6 开关按钮ToggleButton和开关Switch
- 状态开关按钮(ToggleButton)和开关(Switch)的功能与用法
- 开关按钮(ToggleButton&Switch)
- 状态开关按钮(ToggleButton)和开关(Switch)的功能与用法
- 开关按钮(ToggleButton)与开关(Switch)的功能与用法
- Android自学笔记之ToggleButton(开关按钮)的功能、特殊属性、用法
- Android——滚动视图(ScrollView)图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟
- Android的ToggleButton实现开关按钮
- Android——图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟、图片透明度、滚动和时间选择器
- Android开关按钮ToggleButton的使用,可以实现密码框和文本框的切换
- Android的ToggleButton和Switch以及AnalogColok和DigitalColok的用法-android学习之旅(二十)
- Android - Custom - SlidingToggleButton(滑动开关按钮)
- Cocos2d-x学习笔记(12)(CCControlSwitch开关、CCControlSlider滑动条、CCControlButton按钮)
- Toggle Buttons(SwitchButton),开关按钮
- Android ToggleButton Example--开关按钮
- Android ToggleButton Example--开关按钮
- Android中的ToggleButton和Switch开关控件