2013-01-09 51 views
-1

我是android編程中的新手。我看到很多方法可以做到的事件處理,但是當我試圖通過調用處理程序類,它給出處理類名稱錯誤做到這一點:java android處理程序調用

package com.example.test; 

import android.app.Activity; 
import android.content.DialogInterface.OnClickListener; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //attach an instance of HandleClick to the Button 
     findViewById(R.id.button1).setOnClickListener(new HandleClick()); 
    }  

    private class HandleClick implements OnClickListener{ 
     public void onClick(View arg0) { 
      Button btn = (Button)arg0; //cast view to a button 
      // get a reference to the TextView 
      TextView tv = (TextView) findViewById(R.id.textview1); 
      // update the TextView text 
      tv.setText("You pressed " + btn.getText()); 
     } 
    } 
} 

"HandleClick"錯誤來對這個好說類應該是抽象的類型?

我不明白爲什麼它給這個錯誤可以幫助我嗎?

回答

2

這是錯誤的OnClickListener類。你有

import android.content.DialogInterface.OnClickListener; 

您需要:

import android.view.View.OnClickListener; 

對於未來的參考,你得到的錯誤是「類型必須實現繼承的抽象方法......」。這是因爲你需要實現DialogInterface的onClick,這應該導致您注意,這是錯誤的進口(因爲你有onClick(View)

0

使簡單&使用此,

b1 = (Button) findViewById(R.id.button1); 
TextView tv = (TextView) findViewById(R.id.textview1); 
b1.setOnClickListener(new OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     Toast msg = Toast.makeText(getBaseContext(), 
     "You have clicked Button 1", Toast.LENGTH_LONG); 
     msg.show(); 
     tv.setText("You pressed " + btn.getText()); 
    } 
}); 
0

您導入錯誤的OnClickListener,你應該從android.view中導入一個。查看