简体   繁体   English

自定义(圆形)切换按钮

[英]Custom (round) toggle button

I am trying to build a custom toggle button in Android, I want it to look like radio button but function as toggle button. 我正在尝试在Android中构建自定义切换按钮,我希望它看起来像单选按钮但功能为切换按钮。 Can some one help me with this? 有人可以帮我弄这个吗? any clue hints close to answer is appreciated. 任何线索提示接近答案表示赞赏。

You need to override the look of the toggle button via some custom xml 您需要通过一些自定义xml覆盖切换按钮的外观

quick overview: 快速概述:

  • create png files with the look and feel of your buttons, if you like the radio buttons, grab them from ...\\eclipse_android-sdk-windows\\platforms\\android-8\\data\\res\\drawable-hdpi 创建具有按钮外观的png文件,如果你喜欢单选按钮,请从... \\ eclipse_android-sdk-windows \\ platforms \\ android-8 \\ data \\ res \\ drawable-hdpi抓取它们
  • in your layout xml android:background="@drawable/round_toggle" /> 在你的布局xml android:background =“@ drawable / round_toggle”/>

  • add selectors with as many states (up to 6 + default) in round_toggle.xml 在round_toggle.xml中添加具有多个状态(最多6个默认值)的选择器

     <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/roundtoggle_on_pressed" /> <item android:state_checked="true" android:state_focused="true" android:drawable="@drawable/roundtoggle_on_focused" /> <item android:state_checked="true" android:state_focused="false" android:drawable="@drawable/roundtoggle_on_normal" /> <item android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/roundtoggle_off_pressed" /> <item android:state_checked="false" android:state_focused="true" android:drawable="@drawable/roundtoggle_off_focused" /> <item android:state_checked="false" android:state_focused="false" android:drawable="@drawable/roundtoggle_off_normal" /> <item android:drawable="@drawable/roundtoggle_off_normal" /> </selector> 

full details in http://www.anddev.org/act_custom_togglebuttons-t10620.html 详情请见http://www.anddev.org/act_custom_togglebuttons-t10620.html

Why not use the RadioButton view and RadioGroup layout? 为什么不使用RadioButton视图和RadioGroup布局?

RadioButton myRadioButton = (RadioButton)findViewById(R.id.myradiobutton);
myRadioButton.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
          // Toggle the radio button on click.
          RadioButton button = (RadioButton)v;
          button.setChecked(!button.isChecked());                   
     }
});

You could try re-skinning a checkbox. 您可以尝试重新设置一个复选框。 The following link is a tutorial on doing so: 以下链接是关于这样做的教程:

link 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM