简体   繁体   English

如何从其他活动更改“切换/切换”按钮状态

[英]How to change Switch/toggle button state from another activity

Ive done something like this. 我做了这样的事情。 Im receiving 'true' value from 2nd activity and when i receive that it should change my switch button state to OFF. 我从第二个活动中收到“真”值,当我收到它应该将我的开关按钮状态更改为“关”时。 Becouse we need to put manually switch to ON possition. 因为我们需要手动将开关切换到ON位置。 I think it should work but it didnt 我认为应该可以,但是没有

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
        //ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);

        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        final Switch sswitch = (Switch) findViewById(R.id.alarmToggle);
        sswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean val) {
                val = getIntent().getBooleanExtra("toggleBtn", false);
                if(val)
                 {
                    sswitch.setChecked(false);
                }

            }
        });
    }

In 2nd activity as i said im sending that value to the first one 正如我所说,在第二个活动中,我将该值发送给第一个活动

 public void backtomain(View view)
    {
        Intent intent = new Intent(this, AalarmActivity.class);
        intent.putExtra("toggleBtn", true);
        finish();

    }

You can do it as you are asking if isChecked on your first if, with : 您可以在询问第一个条件是否为isChecked时执行以下操作:

((ToggleButton)view).isChecked();

Then in your Intent put this : 然后在您的Intent放入:

intent.putExtras("toggleBtn", ((ToggleButton)view).isChecked());

EDIT 编辑

Acording on your code it will only make the Intent when the ToggleButton is true, isnt' it? 根据您的代码,仅当ToggleButton为true时,它才会产生Intent ,不是吗? Then if it enters on the if it means that the ToggleButton is enabled, so instead of pass ((ToggleButton)view).isChecked() on your putExtras you can pass true instead, then make an else and putting it to false 然后如果在on上输入, if表示已启用ToggleButton ,因此可以代替传递((ToggleButton)view).isChecked()在您的putExtras上传递true ,然后进行else并将其设置为false

EDIT 2 编辑2

I think you forgot the startActivity() on : 我认为您忘记了startActivity()

Intent intent = new Intent(this, AalarmActivity.class);
intent.putExtra("toggleBtn", true);
startActivity(intent);
finish();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个按钮更改切换按钮的状态 - Change state of toggle button from another button 通过单击带有setSelected事件的另一个切换按钮来更改切换按钮的状态 - change state of toggle button by clicking another toggle button with setSelected event 如何使用程序将切换按钮的状态从打开更改为关闭? - how to change the state of the toggle button from on to off using the program …? 如何通过另一个活动中的按钮更改活动的背景色 - How to change background colour of an activity via button from another activity 如何切换到另一个活动以及更改该活动中的片段? - How to switch to another activity as well as change the fragment which is in that Activity? 如何在另一个活动中使用切换按钮停止服务? - How can I stop the service using a toggle button in another activity? 如何通过按下另一个活动按钮上的按钮来永久更改我的按钮颜色 - How to change my button color permanent from pressing button on another activity button 如何通过在 android 工作室中单击按钮从另一个活动中更改活动中的 AsyncTask 变量? - How to change a variable of an AsyncTask in an activity from another activity by a button click in android studio? 如何通过按钮更改Android studio中开关的state? - How do I change the state of a switch in Android studio via a button? 从AlertDialog按钮切换活动 - Switch activity from a AlertDialog button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM