1

这是我的代码,Eclipse 说

Multiple markers at this line
    - Syntax error on token(s), misplaced construct(s)
    - Syntax error on token "OnClickListener", = expected after this token

我不确定我应该怎么做,谢谢

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;



public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

Button btn = (Button) findViewById(R.id.btn);
btn.OnClickListener (new OnClickListener()
);

    public void onClick(View arg0)
    {
    }

;

}
4

4 回答 4

1
// try this
public class MainActivity extends Activity {

    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               // write your button click code here
           }
       });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
于 2013-10-05T09:12:06.977 回答
1

您设置 an 的语法OnClickListener是错误的。正确的语法是

btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) { // You override the onClick method here and this should be within your anonymous OnClickListener class.
        // Code for what happens on click of the button goes here.
    }
});
于 2013-10-05T08:58:01.583 回答
0

你的语法错误,试试这个:

btn.setOnClickListener (new OnClickListener(){

    @Override
    public void onClick(View arg0){
    }

});

OnClickListener 用作匿名类。您必须覆盖大括号中的 onClick 。

于 2013-10-05T08:58:07.843 回答
0

尝试这个,

 btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            //do your stuff


        }
    });
于 2013-10-05T08:58:25.333 回答