录制按钮状态

时间:2013-11-19 21:14:07

标签: android android-xml

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

状态我需要录制按钮:

状态1。默认按钮图像(初始状态)

状态2。当按钮处于按下状态时设置可绘制按钮

状态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;

2 个答案:

答案 0 :(得分:2)

您需要使用ToggleButton控件。

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

(根据你的帖子,我已经将drawables标记为他们对应的状态。)

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

最后声明默认drawable的原因: 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).

示例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" />

示例ToggleButton点击事件:

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

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

答案 1 :(得分:0)

操作系统根据是否按下/单击按钮来处理按钮的选定状态。您无法在代码中设置它。如果您想在发生某些事情时更改按钮,则必须在代码中执行此操作:

case R.id.record_button:
    if (mr.getState() == ExtAudioRecorder.State.READY){
        mr.start();
        record_button.setImageResource(R.drawable.record_button_recording);
    }
    else if(mr.getState() == ExtAudioRecorder.State.RECORDING){
        mr.stop();
        mr.release();

        record_button.setImageResource(R.drawable.record_button_default_state);

        setMediaPlayerPathAndPrepare();
    }