英文:
Getter and Setter in Android
问题
在我的MainActivity中,我有一个开关(Switch)和一个按钮(Button)。在改变开关状态后,一旦点击按钮,我想要获取开关的状态(true/false)。为了做到这一点,我实现了Getter和Setter,但由于不明原因,一旦我设置了值,我始终得到的是false。
MainActivity
Switch profession = (Switch) findViewById(R.id.profswitch);
profession.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
GetSwitch SP = new GetSwitch();
SP.setSetClicked(isChecked);
}
});
Button profButton = findViewById(R.id.btnSwitchNxt);
profButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetSwitch sw = new GetSwitch();
boolean setClicked = sw.isSetClicked();
Log.i("GetSwitch", setClicked + "");
}
});
GetSwitch
public class GetSwitch {
private boolean setClicked;
public void setSetClicked(boolean setClicked) {
this.setClicked = setClicked;
Log.i("SetSwitch", this.setClicked + "");
}
public boolean isSetClicked() {
return setClicked;
}
}
我在这里做错了什么?
英文:
In my MainActivity I have a Switch and a Button, After changing the switch status, Once the button is clicked I want to get the switch status true/false. To do this, I've implemented Getter and Setter, for unknown reason, once I set the value, i'm always getting false.
<br/>
MainActivity
<br/>
Switch profession = (Switch) findViewById(R.id.profswitch);
profession.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
GetSwitch SP = new GetSwitch();
SP.setSetClicked(isChecked);
}
});
Button profButton = findViewById(R.id.btnSwitchNxt);
profButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetSwitch sw = new GetSwitch();
boolean setClicked = sw.isSetClicked();
Log.i("GetSwitch",setClicked+"");
}
});
<br/>
GetSwitch
public class GetSwitch{
private boolean setClicked;
public void setSetClicked(boolean setClicked) {
this.setClicked = setClicked;
Log.i("SetSwitch",this.setClicked+"");
}
public boolean isSetClicked() {
return setClicked;
}
}
What am I doing wrong here ?
答案1
得分: 0
你正在使用不同的对象来设置和获取值。您只需要声明一个GetSwitch对象,其代码应该像下面这样:
GetSwitch SP = new GetSwitch();
Switch profession = (Switch) findViewById(R.id.profswitch);
profession.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SP.setSetClicked(isChecked);
}
});
Button profButton = findViewById(R.id.btnSwitchNxt);
profButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean setClicked = SP.isSetClicked();
Log.i("GetSwitch", setClicked + "");
}
});
英文:
You are using different object for setting and getting value. You just need to declare one GetSwitch object, which has to be like below..
GetSwitch SP = new GetSwitch();
Switch profession = (Switch) findViewById(R.id.profswitch);
profession.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SP.setSetClicked(isChecked);
}
});
Button profButton = findViewById(R.id.btnSwitchNxt);
profButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean setClicked = SP.isSetClicked();
Log.i("GetSwitch",setClicked+"");
}
});
答案2
得分: 0
你正在使用同一个类的两个单独实例来获取相同的属性。当你更新实例的属性时,更改属于该实例。你可以像这样做:
Switch profession = (Switch) findViewById(R.id.profswitch);
GetSwitch sw = new GetSwitch();
profession.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sw.setSetClicked(isChecked);
}
});
Button profButton = findViewById(R.id.btnSwitchNxt);
profButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean setClicked = sw.isSetClicked();
Log.i("GetSwitch", setClicked + "");
}
});
在这里,我正在获取和设置同一实例的属性,以便它们不会改变。你也可以在类的构造函数中定义这个实例,并使用 this.sw
来引用它们,这是一个良好的实践。
英文:
You are using two separate instances of the same class for getting same property. When you are updating a property of an instance that change belongs to that instance. You can do something like this:
Switch profession = (Switch) findViewById(R.id.profswitch);
GetSwitch sw = new GetSwitch();
profession.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
sw.setSetClicked(isChecked);
}
});
Button profButton = findViewById(R.id.btnSwitchNxt);
profButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean setClicked = sw.isSetClicked();
Log.i("GetSwitch",setClicked+"");
}
});
See here I am getting and setting the property of same instance so that they are not changed. You can also define this instance in the constructor of the class and reference them using this.sw
as a good practice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论