【问题标题】:How to make ImageButton behave as ToggleButton如何使 ImageButton 表现为 ToggleButton
【发布时间】:2020-09-11 02:24:33
【问题描述】:

我希望我的 Android 按钮具有一些属性:

  1. 拥有 Android HoloLight 风格
  2. 表现得像 ImageButton - 将图像作为其内容,而不是文本
  3. 表现为切换按钮 - 切换开/关状态

所以我的代码目前看起来像这样:

我的 xml 文件:

<ImageButton
    android:id="@+id/button"
    style="@android:style/Widget.Holo.Light.Button.Toggle"
    android:layout_width="70px"
    android:layout_height="70px"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="5dp"
    android:src="@drawable/icon" />

我的 java 文件:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_file);

    Button button = (ImageButton) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v(log, "button pressed");

            if (skillsButton.isSelected()) {
                Log.v(log, "button is unselected");
                button.setSelected(false);
            } else {
                Log.v(log, "button is selected");
                button.setSelected(true);
            }
        }
    });
}

当我运行我的代码并单击按钮时,我可以看到我的按钮有一个图像作为它的描述,看起来像一个 Android HoloLight ToggleButton,但是当我按下它时它并没有打开和关闭(蓝灯没有打开)。

我可以看到按钮的 isSelected() 状态在 LogCat 上正在发生变化。

有我想要的解决方案吗?

此外,据我所知,Android HoloLight 主题按钮的“颜色”固定为浅灰色。有没有办法把这种颜色改成另一种颜色,最好是白色?

【问题讨论】:

标签: android button themes imagebutton togglebutton


【解决方案1】:

这就是我所做的,因此我可以有 2 或 3 个 Buttons 带有可切换的图像并且位于 RadioGroup 中,因此一次只能选择一个。

将其设为ToggleButton

<ToggleButton
android:id="@+id/button"
style="@android:style/CustomButton"   // Note the style
android:layout_width="70px"
android:layout_height="70px"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="5dp" />

res/styles中创建它引用的样式

<style name="CustomButton" parent="@android:style/Widget.CompoundButton">  // you can change the parent here depending on what you need
    <item name="android:background">@drawable/icon</item>
    <item name="android:textOff"></item>
    <item name="android:textOn"></item>
    //  add other properties if you need
</style>

在我的@drawable/cold_button 中引用了一个选择器,因为它在按下时会发生变化,但您可以简单地成为background 的图像

【讨论】:

    【解决方案2】:

    我的建议是为您的按钮提供可绘制的拖曳。一种状态和一种关闭状态,“on”状态drawable具有右侧蓝色,“off”状态按钮具有左侧蓝色。之后,您必须在按钮上设置 OnTouchListener,如下所示:

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            float x=event.getX();
            if(x<=v.getWidth()/2){
                v.setBackgroundResource(R.drawable.off);
                state=0;
            }else{
                v.setBackgroundResource(R.drawable.on);
                state=1;
            }
        }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多