嘿家伙我是Android的开发新手和项目,我必须实现一个按钮的监听器。 但不幸的是,他无法检测到我认为的按钮。 这里是Java代码:Android Button监听器实现
public class Touch extends AppCompatActivity implements OnTouchListener,OnClickListener {
private button boutonCompteur = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.Compteur);
boutonCompteur = (button) findViewById(R.id.Compteur);
boutonCompteur.setOnTouchListener(this);
boutonCompteur.setOnClickListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
} }
这里是XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="appuyez ici pour compter votre nombre de touch"
android:id="@+id/Compteur"
android:height="130dp"
android:textColor="#1818e3"
android:textColorHighlight="#cd5555"
android:textSize="22dp"
android:textStyle="italic"
android:layout_marginTop="150dp"
android:layout_alignParentStart="true" />
谢谢您的回答:)
您不需要将onTouch和onClick都添加到按钮。只是使用onClick –
试试这个:http://stackoverflow.com/questions/25803727/android-setonclicklistener-method-how-does-it-work(setOnClickListner);或从官方文档:http://developer.android.com/reference/android/widget/Button.html – KickingLettuce
感谢您的答案,这真的很有帮助!我学到的东西感谢你们:) – Matnako