【问题标题】:onClickListener problemsonClickListener 问题
【发布时间】:2014-05-16 18:27:36
【问题描述】:

所以我尝试制作一个简单的图片按钮:

<ImageButton
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/button_image" 
    android:contentDescription="@string/desc/>

xml:

public class MainActivity extends ActionBarActivity {
protected void onCreate1 (Bundle savedInstaceState){
    Bundle savedInstanceState;
    super .onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button1  = (Button) findViewById (R.id.button1);

            View.setOnClickListener(new onClickListener () {
                public void onClick (View v) {
                    Toast.makeText(v.getContext(), "You clicked it. Genius.", Toast.LENGTH_SHORT).show();
                }
            });

但它不断弹出错误:"The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new onClickListener(){})"

我导入了android.view.View.OnClickLIstener;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;

【问题讨论】:

  • button1.setOnClickListener(...
  • 也投射到 ImageButton 本身
  • @dymmeh 出现同样的错误:/

标签: android eclipse button


【解决方案1】:

你应该这样做:

 button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

使用 button1 并检查 OnClickListener 大小写。

【讨论】:

  • 还是同样的错误“View类型中的setOnClickListener(View.OnClickListener)方法不适用于参数(new onClickListener(){})”
  • 您在编译之前是否保存了 Java 文件?错误消息显示“onClickListener”,而您的代码现在应该使用“OnClickListener”。
【解决方案2】:

要使用 onClickListener 使用此代码

        Button bTutorial1 = (Button) findViewById(R.id.tutorial1);
    bTutorial1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


        }
    });

【讨论】:

    【解决方案3】:

    首先要知道的是onclick 处理程序实现了接口View.OnClickListener。您需要创建一个实现接口的类,因为您需要添加执行的代码。 .

    您的onClickListener 应按如下方式使用:

     Button button1  = (Button) findViewById (R.id.button1);
    
            View.setOnClickListener(new onClickListener () {
                public void onClick (View v) {
                    Toast.makeText(v.getContext(), "You clicked it. Genius.", Toast.LENGTH_SHORT).show();
                }
            });
    

    通过如下方式创建类 Inline:

    findViewById(R.id.button1).setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Inform the user the button has been clicked
       }
    

    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2011-05-26
      相关资源
      最近更新 更多