无法解析切换按钮的CompundButton按钮视图



如果Switch关闭,我将尝试禁用EditText。但是我的CompoundButton按钮View无法解析。问题出在哪里?这是我的代码:

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);
}
}
});
}

错误图像代码图像

我已经重写了下面的代码部分。你可以试试看。

@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);
}
}
});
}

然后是其他代码。

最新更新