将onClickListeners添加到按钮时出错

时间:2014-02-14 15:48:38

标签: android button onclicklistener

我正在为app编写一个简单的android程序,因为有2个按钮,我只是单击按钮&单击显示一些消息, 这是我的两个按钮的代码。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonStart = (Button)findViewById(R.id.buttonStart);        
    buttonStart.setOnClickListener(startListener); // Register the onClick listener with             the implementation above

    Button buttonStop = (Button)findViewById(R.id.buttonStop);        
    buttonStop.setOnClickListener(stopListener); // Register the onClick listener with the implementation above
     }


   //Create an anonymous implementation of OnClickListener
    private OnClickListener startListener = new OnClickListener() {
     public void onClick(View v) {
      Log.d(logtag,"onClick() called - start button");              
      Toast.makeText(MainActivity.this, "The Start button was clicked.",          Toast.LENGTH_LONG).show();
      Log.d(logtag,"onClick() ended - start button");
    }
   };

    // Create an anonymous implementation of OnClickListener
    private OnClickListener stopListener = new OnClickListener() {
    public void onClick(View v) {
     Log.d(logtag,"onClick() called - stop button"); 
     Toast.makeText(MainActivity.this, "The Stop button was clicked.", Toast.LENGTH_LONG).show();
      Log.d(logtag,"onClick() ended - stop button");
    } 
    };

我的错误是说“OnClickListener无法解析为类型”

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

  

“OnClickListener无法解析为类型”

需要在当前活动中导入View.OnclickListener:

import android.view.View.OnclickListener;