Android 自定义按钮没有';I don’我不能和你一起工作
我对触控侦听器有问题。我创建了一个自定义按钮。此按钮可与单击事件一起正常工作。但它不适用于此事件 这是我的定制按钮。我只需要两个选项,一个按钮,一个按钮Android 自定义按钮没有';I don’我不能和你一起工作,android,android-custom-view,motionevent,ontouch,Android,Android Custom View,Motionevent,Ontouch,我对触控侦听器有问题。我创建了一个自定义按钮。此按钮可与单击事件一起正常工作。但它不适用于此事件 这是我的定制按钮。我只需要两个选项,一个按钮,一个按钮 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button2_pressed
<?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" />
这是我的另一个按钮。这是正确的工作。此按钮是一个普通按钮,只需单击并播放即可。
当我按下按钮时,按钮2\u pressed出现,当我未按下按钮2时,按钮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();
}
});
我自己又解决了我的问题。让我们解释一下; 首先,我关闭了所有按钮,例如btnPlay1和btnPlay1c 但是按钮之间是不同的。区别仅在于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>
<?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>
此解决方案可能会很累,但它解决了问题。按下按钮后,它将聚焦。我猜选择器中的第二项总是解析为true,因此按下的按钮会一直显示。您可以尝试注释该特定项目以确认。按钮2\u按下不会与此事件一起出现。只有按钮2出现。此自定义按钮代码用于onclick事件。我将我的其他代码添加到上面的文本中。但它不适用于onTouch事件自定义按钮代码中的某些内容可能会有所不同,但我不知道。
<?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>
<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>
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;
}
});