错误setonclickListener未定义类型按钮

时间:2013-09-21 15:36:53

标签: java android eclipse

您好我收到错误:

  

方法setonclickListener(new onclickListener(){})未定义   对于类型按钮

这是我的代码:

public class Sliding extends Activity {
    /** Called when the activity is first created. */
    private LinearLayout MenuList;
    private Button btnToggleMenuList;
    private int screenWidth;
    private boolean isExpanded;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sliding);
        MenuList = (LinearLayout) findViewById(R.id.linearLayout2);
        btnToggleMenuList = (Button) findViewById(R.id.button1);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenWidth = metrics.widthPixels;

        btnToggleMenuList.setonclickListener(new onclickListener() {
            public void onclick(View v) {
                if (isExpanded) {
                    isExpanded = false;
                    MenuList.startAnimation(new CollapseAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
                }else {
                    isExpanded = true;
                    MenuList.startAnimation(new ExpandAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
                }
                }
        });
    }
}

谁能告诉我为什么?

4 个答案:

答案 0 :(得分:1)

替换此

btnToggleMenuList.setonclickListener(new onclickListener() {
        public void onclick(View v) {

通过

btnToggleMenuList.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

同时确保您拥有以下导入声明

import android.view.View.OnClickListener; 

答案 1 :(得分:0)

将其更改为

View.OnClickListener()

答案 2 :(得分:0)

写下这个:

btnToggleMenuList.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

而不是:

btnToggleMenuList.setonclickListener(new onclickListener() {
    public void onclick(View v) {

答案 3 :(得分:0)

试试这个:

  btnToggleMenuList.setonclickListener(new onclickListener() {

        @Override
        public void onclick(View v) {