gpt4 book ai didi

android - 录制按钮状态

转载 作者:行者123 更新时间:2023-11-29 21:27:06 25 4
gpt4 key购买 nike

我正在创建录制按钮,我需要这些状态,但我不知道如何使用 xml 实现它们。我尝试过激活、选择和按下状态,但这些都没有完成工作。我已经创建了所有可绘制对象,但各州把我搞砸了。

声明我需要录制按钮来执行以下操作:

状态1.默认按钮图片(初始状态)

状态2.设置按钮在按下过程中的drawable

状态 3. 按下按钮后(按下后)设置按钮的可绘制状态,我们现在处于录制模式

状态4.再次点击按钮后回到初始状态

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/record_button_default_state" />

<item
android:state_selected="true"
android:drawable="@drawable/record_button_recording" />

<item
android:state_selected="false"
android:drawable="@drawable/record_button_default_state" />

<item
android:state_pressed="true"
android:drawable="@drawable/record_button_pressed_state" />

OnClickListener()

case R.id.record_button:
if (mr.getState() == ExtAudioRecorder.State.READY){
mr.start();
record_button.setSelected(true);

}
else if(mr.getState() == ExtAudioRecorder.State.RECORDING){
mr.stop();
mr.release();

record_button.setSelected(false);

setMediaPlayerPathAndPrepare();


}
break;

最佳答案

您需要使用 ToggleButton控制。

这是您需要调用的选择器代码:

(根据您的帖子,我已将可绘制对象标记为它们对应的状态)。

*tb_selector.xml*

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/state4"
android:state_checked="true"
android:state_pressed="true" />
<item
android:drawable="@drawable/state2"
android:state_pressed="true" />
<item
android:drawable="@drawable/state3"
android:state_checked="true" />
<item
android:drawable="@drawable/state1" />
</selector>

最后声明默认可绘制对象的原因: Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your default value should always be last (as demonstrated in the following example).

sample ToggleButton XML:

<ToggleButton 
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"
android:onClick="onToggleClicked"
android:background="@drawable/tb_selector" />

sample ToggleButton点击事件:

public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();

if (on) {
// Enable vibrate
} else {
// Disable vibrate
}
}

关于android - 录制按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20082170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com