我怎么知道另一个活动的ToggleButton的状态?

时间:2015-03-02 09:20:57

标签: java android

如何从其他活动中了解ToggleButton的状态?

我有课

public class MainScreen extends Activity{   
    protected ToggleButton tgbutton;    
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.mainscreen);           
       tgbutton = (ToggleButton)findViewById(R.id.advice); 
       SharedPreferences sharedPreferences = PreferenceManager
               .getDefaultSharedPreferences(getApplicationContext());
       tgbutton.setChecked(sharedPreferences.getBoolean("toggleButton", false)); 
    }        
    public void oneClick(View v) { 
        if(tgbutton.isChecked() == false) { playSound(mSound); }
        else { pauseSound(mSound); }  
        }
    }

public class SmallprintA extends Activity {     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
       setContentView(R.layout.smallprint_a);
    OnClickListener SendOnClickListener = new OnClickListener() {   
        @Override
        public void onClick(View v) {           
            if(tgbutton.isChecked() == true){ playSound(mEmpty); }
            else { playSound(mEmpty); }
            }
        };  
}

我需要在类SmallprintA中确定ToggleButton tgbutton的状态。没有类继承可以这样做吗?

1 个答案:

答案 0 :(得分:0)

我确实tgbutton是静态的

   public class MainScreen extends Activity{   
            protected static ToggleButton tgbutton;    // STATIC
            public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.mainscreen);           
               tgbutton = (ToggleButton)findViewById(R.id.advice);                    
            }        
            public void oneClick(View v) { 
                if(tgbutton.isChecked() == false) { playSound(mSound); }
                else { pauseSound(mSound); }  
                }
            }

public class SmallprintA extends Activity {     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
       setContentView(R.layout.smallprint_a);
    OnClickListener SendOnClickListener = new OnClickListener() {   
        @Override
        public void onClick(View v) {           
            if(MainScreen.tgbutton.isChecked() == true){ playSound(mEmpty); }
            else { playSound(mEmpty); }
            }
        };  
}