无法解析我的CompundButton buttonview的“切换”按钮

时间:2018-12-01 01:41:18

标签: java android

如果开关关闭,我试图禁用EditText,我为此做了If-else。 但是我的CompoundButton buttonView无法解析。可能是什么问题? 这是我的代码:

public class MainActivity extends AppCompatActivity {

        public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        EditText editText = (EditText) findViewById(R.id.editText);
        /** Called when the user taps the Send button */
        public void sendMessage(View view) {
            // Do something in response to button
            Intent newxml = new Intent(this, DisplayMessageActivity.class);
            String message = editText.getText().toString();
            newxml.putExtra(EXTRA_MESSAGE, message);
            startActivity(newxml);
            TextView displayText = (TextView) findViewById(R.id.displayText);
            displayText.setText(message);
            String someText = "Your Message";
            TextView yourMessage = (TextView) findViewById(R.id.yourMessage);
            yourMessage.setText(someText);
        }
        final Switch switch1 = (Switch) findViewById(R.id.switch1);
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // true if the switch is in the On position
                if(isChecked){
                    editText.setFocusableInTouchMode(true);
                    editText.setFocusable(true);
                }else{
                    editText.setFocusableInTouchMode(false);
                    editText.setFocusable(false);
                }
            }
        });
    }

Error image Code Image

1 个答案:

答案 0 :(得分:0)

我已经重写了下面的代码部分。您可以在最后尝试一下。

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

        final Switch switch1 = (Switch) findViewById(R.id.switch1);
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // true if the switch is in the On position
                if(isChecked){
                    editText.setFocusableInTouchMode(true);
                    editText.setFocusable(true);
                }else{
                    editText.setFocusableInTouchMode(false);
                    editText.setFocusable(false);
                }
            }
        });

    }

...然后是其他代码。