2013-07-02 406 views
0

我創建了5個類。一個類是主要活動,其餘是意圖活動...我實現OnClickListener接口直接到類。而當運行android項目按鈕事件不工作。源代碼是下面給出。按鈕onClickView不工作,而實現OnClickListener

package com.sooraj.cbsroot; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class HomeMainActivity extends Activity implements OnClickListener { 
    Button btnBus, btnAbout, btnContact, btnAdmin; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home_main); 
     btnBus = (Button) findViewById(R.id.btnBus); 
     btnAbout = (Button) findViewById(R.id.btnAbout); 
     btnContact = (Button) findViewById(R.id.btnContact); 
     btnAdmin = (Button) findViewById(R.id.btnAdmin); 

    } 

    @Override 
    public void onClick(View v) { 
     if (v.getId() == R.id.btnBus) { 
      Intent intentBus = new Intent(HomeMainActivity.this, 
        BusDetails.class); 
      startActivity(intentBus); 
     } 

     if (v.getId() == R.id.btnAbout) { 
      Intent intentAbout = new Intent(HomeMainActivity.this, About.class); 
      startActivity(intentAbout); 
     } 

     if (v.getId() == R.id.btnContact) { 
      Intent intentContact = new Intent(HomeMainActivity.this, 
        Contact.class); 
      startActivity(intentContact); 
     } 

     if (v.getId() == R.id.btnAdmin) { 
      Intent intentAdmin = new Intent(HomeMainActivity.this, 
        AdminLogin.class); 
      startActivity(intentAdmin); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.home_main, menu); 
     return true; 
    } 

} 
+1

首先爲所有按鈕註冊onClickListner。然後點擊事件將起作用。 –

回答

2

你忘了註冊OnClickListener

btnBus.setOnClickListener(this); 

你必須爲每個Button例如

5

做此操作請使用

btnBus.setOnClickListener(this); 
    btnAbout.setOnClickListener(this); 
    btnContact.setOnClickListener(this); 
    btnAdmin.setOnClickListener(this); 
在onCreate方法