gpt4 book ai didi

java - 无法解析我的 CompundButton buttonview for Switch button

转载 作者:行者123 更新时间:2023-11-30 10:08:54 25 4
gpt4 key购买 nike

我试图在 Switch 关闭时禁用 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

最佳答案

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

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

}

.....然后是其他代码。

关于java - 无法解析我的 CompundButton buttonview for Switch button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53567063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com