切换按钮setOnClickListener无法解析 [英] toggle button setOnClickListener can not resolve

查看:760
本文介绍了切换按钮setOnClickListener无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid编程和我得到这个从如何正确​​地实施这一开发者网站。然而,当我复制并粘贴到机器人工作室这可能无法解决setOnClickListener,setOnCheckedChangeListener和buttonView。

这是我第一次在与Android的切换按钮和按键工作,我做了很多在这里寻找关于之前,我打破了问这个。这是MainInterface的一个单独的按钮类之外我是否需要扩展或实现任何特殊或导入什么?

 进口android.content.Context;
 进口android.net.ConnectivityManager;
 进口android.net.NetworkInfo;
 进口android.view.View;
 进口android.widget.CompoundButton;
 进口android.widget.ToggleButton; 公共类按钮扩展MainInterface {    切换按钮切换=(切换按钮)findViewById(R.id.BeaconButton);
    toggle.setOnClickListener(新CompoundButton.OnCheckedChangeListener());
    toggle.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
        公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
            如果(器isChecked){
                //启用的切换
            }其他{
                //肘被禁用
            }
        }
    });
}

另外是上述code和下面code之间的差。将以下code设置监听器适用?

 公共无效onToggleClicked(查看视图){
    //是切换呢?
    Boolean在=((切换按钮)视图).isChecked();
    如果(上){
        // 做一点事
        }    }其他{
        //设置,因为它是
    }
}


解决方案

setOnClickListener 不接受 CompoundButton.OnCheckedChangeListener()作为一名听众从而给你编译时错误。

setOnClickListener

方法签名

  setOnClickListener(View.OnClickListener升)

解决方案:

使用默认的 View.OnClickListener 侦听器视图中设置你的切换按钮

  toggle.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根            }
        });

I am new to android programming and i got this from the developer website on how to properly implement this. However when i copy and pasted this into android studio it could not resolve setOnClickListener, setOnCheckedChangeListener, and buttonView.

This is my first time working with toggle buttons and buttons in android and i did a lot of searching about on here before i broke down to ask this. This is a separate button class outside of MainInterface do i need to extend or implement anything special or import anything?

 import android.content.Context;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.view.View;
 import android.widget.CompoundButton;
 import android.widget.ToggleButton;        

 public class Button extends MainInterface {

    ToggleButton toggle = (ToggleButton) findViewById(R.id.BeaconButton);
    toggle.setOnClickListener(new CompoundButton.OnCheckedChangeListener());
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // The toggle is enabled
            } else {
                // The toggle is disabled
            }
        }
    });
}

Also what is the difference between the above code and the following code. would the following code be applicable for setting listeners?

public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();
    if (on) {
        // do something
        }

    } else {
        // set as it was
    }
}

解决方案

The setOnClickListener does not accepts CompoundButton.OnCheckedChangeListener() as a listener thus giving you compile time error.

The method signature for setOnClickListener

setOnClickListener(View.OnClickListener l)

solution:

Use the default View.OnClickListener listener for views to be set in your ToggleButton

 toggle.setOnClickListener(new OnClickListener() {

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

            }
        });

这篇关于切换按钮setOnClickListener无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆