自定义按钮示例不起作用

Pan*_*ana 5 android

我是android应用程序开发的新手.我正在使用http://developer.android.com/resources/tutorials/views/hello-formstuff.html上给出的Form Stuff示例.在这里我使用了自定义按钮示例.

我的代码如下:

    package com.example.helloformstuff;

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

    public class HelloFormStuff extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
            }
        });    

    }
   }
Run Code Online (Sandbox Code Playgroud)

它显示以下错误:

The type new DialogInterface.OnClickListener(){} must implement the inherited 
     abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int)
    - The method setOnClickListener(View.OnClickListener) in the type View is not 
     applicable for the arguments (new DialogInterface.OnClickListener(){})
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚出现这种错误的原因.

请帮帮我.

谢谢

潘卡

Pan*_*ana 1

将导入 android.content.DialogInterface.OnClickListener 替换为

导入 android.view.View.OnClickListener

其余代码保持不变将完美执行代码。