【问题标题】:Android - change textSize based on button pressed/unpressed stateAndroid - 根据按钮按下/未按下状态更改 textSize
【发布时间】:2020-09-15 20:56:54
【问题描述】:

我有一个按钮背景,如果状态被按下或不被按下,它就会改变drawable。我可以像下面这样更改文本颜色和背景,但我想更改文本大小。 .XML 端怎么办?

drawable/button_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/toggle_pressed" android:state_selected="true" />
<item android:drawable="@drawable/toggle_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/toggle_unpressed" />
</selector>

res/color/src_text:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false" android:color="@color/blue2"/>
<item android:state_pressed="true" android:color="@color/color_concrete"/>
<item android:state_pressed="false" android:state_selected="true" 
android:color="@color/color_concrete"/>
<item android:color="@color/blue2" />
</selector>

按钮:

<Button
            android:id="@+id/playButton"
            android:layout_width="200dp"
            android:layout_height="70dp"
            android:background="@drawable/button_selector"
            android:onClick="@{(v)->mainViewModel.playButtonClick(v)}"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            android:text="@string/play"
            android:textColor="@color/src_text"
            android:textSize="40sp"
            android:visibility="@{mainViewModel.observableVisibilityPlayButton ? v.VISIBLE : v.GONE}"/>

【问题讨论】:

  • 我认为那里不可能直接实现。 Check this 看看如何使用动画选择器来做到这一点。

标签: android


【解决方案1】:

您可以创建 Button 的子类并覆盖 onTouch 方法

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // button pressed, change textSize
                return true;
            case MotionEvent.ACTION_UP:
                // button released, revert textSize
                return true;
        }
        return false;
    }

【讨论】:

  • 我可以在哪一部分覆盖它?
  • 您创建从类Button(或AppCompatButton)扩展的类,在该类中覆盖onTouch方法,如上例,然后使用该类代替Button
【解决方案2】:

由于您使用的是数据绑定,因此您可能可以创建一个 BindingAdapter,如下所示:

@JvmStatic
@BindingAdapter("textSize")
fun bindTextSize(button: Button, size: Int) {
    
    if(button.isPressed())
       button.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}

还在视图模型(大小)中保留一个 MutableLiveData 字段,并根据需要更改值。

<Button
            textSize="@{mainViewModel.size}
            android:id="@+id/playButton"
            android:layout_width="200dp"
            android:layout_height="70dp"
            android:background="@drawable/button_selector"
            android:onClick="@{(v)->mainViewModel.playButtonClick(v)}"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            android:text="@string/play"
            android:textColor="@color/src_text"
            android:textSize="40sp"
            android:visibility="@{mainViewModel.observableVisibilityPlayButton ? v.VISIBLE : v.GONE}"/>

【讨论】:

  • 是的,我可以,但按钮不是开关盒,我正在模拟按钮单击动画,如果在按下状态下按下按钮,则需要调整文本大小,如果未按下则正常
  • @DoctorWho 请看我的编辑,这应该可行。
  • 在我的 viewModel 中,我可以定义 int“大小”,但我无法获得按钮触摸事件。如果我想使用数据绑定,我需要在我的视图模型中使用触摸事件
  • 为什么不能在视图模型中获得按钮触摸事件?您已经在访问该事件 android:onClick="@{(v)-&gt;mainViewModel.playButtonClick(v)}" 在 playButtonClick 中,只需根据您的情况更改大小变量。
  • 因为点击动作改变了片段。如果我触摸按钮 drawable/button_selector.xml 并根据需要更改文本颜色
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 2015-06-10
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
相关资源
最近更新 更多