切换按钮的问题
Problems by Toggle Button
我的 toggleButton 不能正常工作,我该怎么办??
如果我按下 toggleButton1 然后最终按下 toggelButton2,因为它没有获得 toggelButton1 的代码。
但是 toggelButton2 运行良好
请任何人帮助我,解决我的问题
ToggleButton toggle1, toggle2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_act);
toggle1= (ToggleButton) findViewById(R.id.toggleButton1);
toggle2= (ToggleButton) findViewById(R.id.toggleButton2);
SharedPreferences spref = getSharedPreferences(null, 0);
if(spref.getBoolean("one", false)) {
toggle1.setChecked(true);
toggle2.setEnabled(false);
} else if(spref.getBoolean("two", false)) {
toggle2.setChecked(true);
toggle1.setEnabled(false);
}
toggle1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
toggle2.setEnabled(!isChecked);
getSharedPreferences(null, 0).edit()
.putBoolean("one", isChecked)
.apply();
startService(new Intent(MainActActivity.this,LockScreenService.class));
}
});
toggle2.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
toggle1.setEnabled(!isChecked);
getSharedPreferences(null, 0).edit()
.putBoolean("two", isChecked)
.apply();
startService(new Intent(MainActActivity.this,PinLockService.class)); }}); }}
SharedPreferences spref = getSharedPreferences(mContext, MODE_PRIVATE);
if(spref.getBoolean("one", false)) {
toggle1.setChecked(true);
toggle2.setChecked(false);
} else if(spref.getBoolean("two", false)) {
toggle2.setChecked(true);
toggle1.setChecked(false);
}
toggle1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked){
spref.edit()
.putBoolean("one", isChecked).commit();
startService(new Intent(MainActActivity.this,LockScreenService.class));
}
}
});
toggle2.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
spref.edit()
.putBoolean("two", isChecked).commit();
startService(new Intent(MainActActivity.this,PinLockService.class));
}
}
}}); }}
我的 toggleButton 不能正常工作,我该怎么办?? 如果我按下 toggleButton1 然后最终按下 toggelButton2,因为它没有获得 toggelButton1 的代码。 但是 toggelButton2 运行良好 请任何人帮助我,解决我的问题
ToggleButton toggle1, toggle2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_act);
toggle1= (ToggleButton) findViewById(R.id.toggleButton1);
toggle2= (ToggleButton) findViewById(R.id.toggleButton2);
SharedPreferences spref = getSharedPreferences(null, 0);
if(spref.getBoolean("one", false)) {
toggle1.setChecked(true);
toggle2.setEnabled(false);
} else if(spref.getBoolean("two", false)) {
toggle2.setChecked(true);
toggle1.setEnabled(false);
}
toggle1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
toggle2.setEnabled(!isChecked);
getSharedPreferences(null, 0).edit()
.putBoolean("one", isChecked)
.apply();
startService(new Intent(MainActActivity.this,LockScreenService.class));
}
});
toggle2.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
toggle1.setEnabled(!isChecked);
getSharedPreferences(null, 0).edit()
.putBoolean("two", isChecked)
.apply();
startService(new Intent(MainActActivity.this,PinLockService.class)); }}); }}
SharedPreferences spref = getSharedPreferences(mContext, MODE_PRIVATE);
if(spref.getBoolean("one", false)) {
toggle1.setChecked(true);
toggle2.setChecked(false);
} else if(spref.getBoolean("two", false)) {
toggle2.setChecked(true);
toggle1.setChecked(false);
}
toggle1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked){
spref.edit()
.putBoolean("one", isChecked).commit();
startService(new Intent(MainActActivity.this,LockScreenService.class));
}
}
});
toggle2.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
spref.edit()
.putBoolean("two", isChecked).commit();
startService(new Intent(MainActActivity.this,PinLockService.class));
}
}
}}); }}