多开关开关柜解决方案

时间:2017-10-27 04:21:46

标签: java android

在此代码中

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_messagesGet_callLog将同时调用我打开或关闭的时间,但我只想打开电话,我可以做什么?

4 个答案:

答案 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*/
       } 
}