【问题标题】:Issues with OnClick Listener for Custom Dialog Box自定义对话框的 OnClick 侦听器问题
【发布时间】:2011-10-11 18:42:18
【问题描述】:

我正在尝试编写一个从用户那里获取名称的自定义对话框。我在 Eclipse 中收到“无法将 OnClickListener 解析为类型 - View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数 (new OnClickListener(){})”错误。有谁知道我做错了什么?

这是我的代码:

public void getName(){

         Dialog dialog = new Dialog(main.this);
         dialog.setContentView(R.layout.customdialog);
         dialog.setTitle("New Game");
         dialog.setCancelable(true);
         //there are a lot of settings, for dialog, check them all out!
         final EditText inputBox = new EditText(this);

         //set up text
         final TextView text = (TextView) dialog.findViewById(R.id.TextView01);
         text.setText("Enter Your Name...");


         //set up button
         final Button button = (Button) dialog.findViewById(R.id.namebutton);
         button.setOnClickListener(new OnClickListener() {
         public void onClick() {
             String str = inputBox.getText().toString(); 
             setName(str);
             }
         });
         //now that the dialog is set up, it's time to show it    
         dialog.show();
     }

【问题讨论】:

    标签: android


    【解决方案1】:

    您可能只需要更改此设置

    button.setOnClickListener(new OnClickListener() {
    

    到这里

    button.setOnClickListener(new View.OnClickListener() {
    

    编辑 - 为了结合我们的答案,还请确保您导入了 Cristian 所说的正确类。

    【讨论】:

      【解决方案2】:

      我猜你导入了错误的OnClickListener。确保你有:

      import android.view.View.OnClickListener;
      

      而不是

      import android.content.DialogInterface.OnClickListener;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-26
        • 1970-01-01
        • 1970-01-01
        • 2017-09-30
        • 1970-01-01
        • 2023-03-07
        相关资源
        最近更新 更多