自定义(圆形)切换按钮

发布于 2024-09-15 11:05:37 字数 89 浏览 3 评论 0原文

我正在尝试在 Android 中构建一个自定义切换按钮,我希望它看起来像单选按钮,但功能充当切换按钮。有人可以帮我解决这个问题吗?任何接近答案的线索提示都值得赞赏。

I am trying to build a custom toggle button in Android, I want it to look like radio button but function as toggle button. Can some one help me with this? any clue hints close to answer is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身骄傲 2024-09-22 11:05:37

您需要通过一些自定义 xml 快速概述来覆盖切换按钮的外观

  • 使用按钮的外观和感觉创建 png 文件,如果您喜欢单选按钮,请从 ...\eclipse_android-sdk-windows\platforms 获取它们\android-8\data\res\drawable-hdpi
  • 在布局 xml 中

    android:background="@drawable/round_toggle" />

  • 在 round_toggle.xml 中添加具有尽可能多状态的选择器(最多 6 个 + 默认值)

    
    <选择器 xmlns:android="http://schemas.android.com/apk/res/android">
    <项目 android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/roundtoggle_on_pressed" />
    
           
    
    
    
    >
    
    

http://www.anddev.org/act_custom_togglebuttons-t10620.html

You need to override the look of the toggle button via some custom 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
  • in your layout xml

    android:background="@drawable/round_toggle" />

  • add selectors with as many states (up to 6 + default) in round_toggle.xml

    <?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

蓝眼睛不忧郁 2024-09-22 11:05:37

为什么不使用 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());                   
     }
});

Why not use the RadioButton view and RadioGroup layout?

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());                   
     }
});
丘比特射中我 2024-09-22 11:05:37

您可以尝试重新设置复选框的外观。以下链接是有关执行此操作的教程:

链接

You could try re-skinning a checkbox. The following link is a tutorial on doing so:

link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文