自定义按钮不适用于OnTouchListener

时间:2015-03-08 14:09:30

标签: android android-custom-view motionevent ontouch

我遇到 OnTouchListener 的问题。我创建了一个自定义按钮。此按钮适用于 onClick事件。但它不适用于 onTouch事件

这是我的自定义按钮。我只想要两个选项,只需按钮和按钮。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button2_pressed"
      android:state_pressed="true" />
<item android:drawable="@drawable/button2_pressed"
      android:state_focused="true" />
<item android:drawable="@drawable/button2" />

这是我的 Ontouch按钮代码。当我按住按钮时,音频正在播放和循环播放。当我释放按钮时,音频停止。此代码正常工作但它不适用于我的自定义按钮。所以当我把手指放在这个按钮上时,当我松开按钮或者我按下按钮时,在每种情况下这个按钮只显示button2.png(在自定义按钮代码上方)

pl6.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    int eventaction = event.getAction();
    switch (eventaction) {
        case MotionEvent.ACTION_DOWN: 

            n6=MediaPlayer.create(MainActivity.this, R.raw.audio4);
            n6.start();
            n6.setLooping(true);
        return true;

        case MotionEvent.ACTION_UP:   
       n6.stop();
       n6.release();

        break;
    }

    return false;

}


});

这是我的另一个按钮。这是正确的工作。此按钮是一个普通按钮,只需单击即可播放。 当我按下按钮时,出现按钮2_pressed,当我按下按钮时,出现按钮2。

play2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         stopPlaying2();    
    m2=MediaPlayer.create(MainActivity.this, R.raw.sound2);
    m2.start();



    }
});   

1 个答案:

答案 0 :(得分:1)

我自己再次解决了我的问题。让我们解释一下; 首先我克隆了所有按钮,例如btnPlay1和btnPlay1c 但按钮xml彼此不同。区别仅在于id和自定义按钮xml文件。我使用了下面显示的2个自定义按钮xml。

首次自定义按钮 mybutton1.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button"
      android:state_pressed="true" />
<item android:drawable="@drawable/button"
      android:state_focused="true" />
<item android:drawable="@drawable/button" />
</selector>

第二个自定义按钮 mybutton1c.xml

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
      android:state_pressed="true" />
<item android:drawable="@drawable/button_pressed"
      android:state_focused="true" />
<item android:drawable="@drawable/button_pressed" />
</selector>

以我的按钮为例。 克隆按钮不可见。(btnPlay2c)

 <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/btnPlay2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/mybutton1" />

                <Button
                    android:id="@+id/btnPlay2c"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/mybutton1c"
                    android:visibility="gone" />

            </FrameLayout>

最后,这是我的java文件。当我按下按钮时,显示按钮克隆,当我按下正常按钮时显示。就是这样。

pl10.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    int eventaction = event.getAction();
    switch (eventaction) {
        case MotionEvent.ACTION_DOWN: 

            n10=MediaPlayer.create(MainActivity.this, R.raw.audio7);
            n10.start();
            n10.setLooping(true);
           ///focus here\\              
            pl10c.setVisibility(View.VISIBLE);
            return true;

        case MotionEvent.ACTION_UP:   
       n10.stop();
       n10.release();
      //and here\\\        
     pl10c.setVisibility(View.INVISIBLE);
        break;
    }

    return false;

}


});

这个解决方案可能很累,但它可以解决问题。

相关问题