在此代码中
CompoundButton.OnCheckedChangeListener multiListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
switch (v.getId()){
case R.id.msg:
startActivity(new Intent(Mainfunction.this, Get_messages.class));
break;
case R.id.cnt:
Toast.makeText(Mainfunction.this, "Contact In Procces", Toast.LENGTH_SHORT).show();
break;
case R.id.call:
startActivity(new Intent(Mainfunction.this, Get_callLog.class));
break;
case R.id.loc:
Toast.makeText(Mainfunction.this, "Location In Procces", Toast.LENGTH_SHORT).show();
break;
case R.id.ring:
Toast.makeText(Mainfunction.this, "Ringing In Proccess", Toast.LENGTH_SHORT).show();
break;
}
}
};
//on each switch
((Switch) findViewById(R.id.msg)).setOnCheckedChangeListener(multiListener);
((Switch) findViewById(R.id.cnt)).setOnCheckedChangeListener(multiListener);
((Switch) findViewById(R.id.call)).setOnCheckedChangeListener(multiListener);
((Switch) findViewById(R.id.loc)).setOnCheckedChangeListener(multiListener);
((Switch) findViewById(R.id.ring)).setOnCheckedChangeListener(multiListener);
在切换案例中,我的意图活动Get_messages
和Get_callLog
将同时调用我打开或关闭的时间,但我只想打开电话,我可以做什么?
答案 0 :(得分:1)
如果您只想在项目被选中时执行某些操作,则可以将整个switch
语句包含在if
内:
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
if (isChecked) {
switch (v.getId()){
// cases go here
}
}
}
答案 1 :(得分:1)
CompoundButton.OnCheckedChangeListener multiListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
if (isChecked) {
switch (v.getId()){
case R.id.msg:
startActivity(new Intent(Mainfunction.this, Get_messages.class));
break;
case R.id.cnt:
Toast.makeText(Mainfunction.this, "Contact In Procces", Toast.LENGTH_SHORT).show();
break;
case R.id.call:
startActivity(new Intent(Mainfunction.this, Get_callLog.class));
break;
case R.id.loc:
Toast.makeText(Mainfunction.this, "Location In Procces", Toast.LENGTH_SHORT).show();
break;
case R.id.ring:
Toast.makeText(Mainfunction.this, "Ringing In Proccess", Toast.LENGTH_SHORT).show();
break;
}
}
}
};
//on each switch
((Switch) findViewById(R.id.msg)).setOnCheckedChangeListener(multiListener);
((Switch) findViewById(R.id.cnt)).setOnCheckedChangeListener(multiListener);
((Switch) findViewById(R.id.call)).setOnCheckedChangeListener(multiListener);
((Switch) findViewById(R.id.loc)).setOnCheckedChangeListener(multiListener);
((Switch) findViewById(R.id.ring)).setOnCheckedChangeListener(multiListener);
答案 2 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViewById(R.id.msg)).setOnCheckedChangeListener(this);
findViewById(R.id.cnt)).setOnCheckedChangeListener(this);
findViewById(R.id.call)).setOnCheckedChangeListener(this);
findViewById(R.id.loc)).setOnCheckedChangeListener(this);
findViewById(R.id.ring)).setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
if (isChecked) {
switch (v.getId()){
case R.id.msg:
startActivity(new Intent(Mainfunction.this, Get_messages.class));
break;
case R.id.cnt:
Toast.makeText(Mainfunction.this, "Contact In Procces", Toast.LENGTH_SHORT).show();
break;
case R.id.call:
startActivity(new Intent(Mainfunction.this, Get_callLog.class));
break;
case R.id.loc:
Toast.makeText(Mainfunction.this, "Location In Procces", Toast.LENGTH_SHORT).show();
break;
case R.id.ring:
Toast.makeText(Mainfunction.this, "Ringing In Proccess", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
答案 3 :(得分:0)
检查复合按钮是否已选中。使用if()循环。
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
if (isChecked) {
/*your code here*/
}
}