Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从其他活动中了解切换按钮的状态?_Java_Android - Fatal编程技术网

Java 如何从其他活动中了解切换按钮的状态?

Java 如何从其他活动中了解切换按钮的状态?,java,android,Java,Android,如何从其他活动中了解切换按钮的状态 我有课 public class MainScreen extends Activity{ protected ToggleButton tgbutton; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainscreen);

如何从其他活动中了解切换按钮的状态

我有课

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); }  
        }
    }


我需要在SmallprintA类中确定ToggleButton tgbutton的状态。这在没有类继承的情况下是可能的?

我不知道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); }  
                }
            }


为另一个类显示代码的某些相关部分?你是说从另一个活动中?顺便说一句,您可以将状态存储在sharedPreferences@P.Dm:使用SmallPrint中的意图发送切换状态MainScreen中的活动足以生成静态ToogleButton tgbuton。谢谢大家
   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); }
            }
        };  
}