嘿傢伙我是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