简体   繁体   English

无法解析“ setEnabled”方法。 Android工作室

[英]Cannot resolve 'setEnabled' method. Android studio

I am pretty new to android studio. 我是android studio的新手。 So I apologize for the newbie kind of question. 因此,对于新手问题,我深表歉意。 I have been reading posts on how to use the setEnabled(); 我一直在阅读有关如何使用setEnabled()的文章。 method to enable and disable a bottom. 启用和禁用底部的方法。 As you will be able to see in the code, I am trying to enable a previously disabled button (through xml using android:enabled="false"/> ) Whenever one of the Radiobuttons in a radioGroup is selected. 正如您将在代码中看到的那样,无论何时选择radioGroup中的单选按钮之一,我都会尝试启用以前禁用的按钮(通过xml使用android:enabled="false"/> )。

The 'setEnabled' part of the code gets highlighted in red and errors pop: cannot resolve method ' setEnabled(boolean) ' 代码的'setEnabled'部分以红色突出显示,并弹出错误消息:无法解析方法' setEnabled(boolean) '

Here is the code. 这是代码。 I'd like to know why this is not working and how to fix it, rather than just the answer itself. 我想知道为什么它不起作用以及如何解决它,而不仅仅是答案本身。 Thank you in advance 先感谢您

    public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radioButtonYes:
            if (checked)
                R.id.buttonNEXT.setEnabled(true);
            break;
        case R.id.radioButtonNo:
            if (checked)
                R.id.buttonNEXT.setEnabled(true);
                break;
        case R.id.radioButtonDontknow:
            if (checked)
                R.id.buttonNEXT.setEnabled(true);
                break;
    }

Don't

R.id.buttonNEXT.setEnabled(true);

DO

((Button) findViewById(R.id.buttonNEXT)).setEnabled(true); 

Finally : 最后

switch(view.getId()) {
    case R.id.radioButtonYes:
        if (checked)
            ((Button) findViewById(R.id.buttonNEXT)).setEnabled(true); 
            break;

What you are trying to use setEnabled() with an int , which is not possible as R.id.someId is of int type. 您正在尝试将setEnabled()int一起使用,这是不可能的,因为R.id.someIdint类型的。 Better to find a view for that id and then use setEnabled() . 最好找到该ID的视图,然后使用setEnabled()

This will work fine, 这样就可以了

((Button) findViewById(R.id.buttonNEXT)).setEnabled(true);

this is wrong 这是错的

R.id.buttonNEXT.setEnabled(true);

R.id.buttonNEXT is the id (just an integer) that you have to use to create a Button R.id.buttonNEXT是您用来创建Button的id (只是一个整数)

you should need to do something like 你应该要做一些类似的事情

 Button myButton = (Button)findViewById(R.id.buttonNEXT);

THEN after that you can invoke all the method a Buttom has 然后,您可以调用Buttom拥有的所有方法

if (checked)
      myButton.setEnabled(true);

You should use it like this 你应该这样使用

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();
    Button b=(Button)findViewById(R.id.buttonNEXT); //casted to button. use the type is used
    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radioButtonYes:
            if (checked)
                b.setEnabled(true); //change to this
            break;
        case R.id.radioButtonNo:
            if (checked)
                b.setEnabled(true); //change to this
                break;
        case R.id.radioButtonDontknow:
            if (checked)
                b.setEnabled(true); //change to this
                break;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM