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

を設定するための構文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 に答える