B
B
bychok3002016-12-17 14:06:51
Android
bychok300, 2016-12-17 14:06:51

How to switch to a new Activity from Switch?

There is a piece of code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //init switch
        Switch swi = (Switch) findViewById(R.id.switch1);
        swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {   
                if (isChecked){ }
                else { }

            }
        });

    }
    protected void ClickMe(View v){
        
            //init editText
            EditText userEditText = (EditText) findViewById(R.id.editText);

            //new activity
            Intent intent = new Intent(MainActivity.this, Parsed.class);

            // в ключ url пихаем текст из первого текстового поля
            intent.putExtra("url", userEditText.getText().toString());
            
            //start new activity
            startActivity(intent);
        }
    }

I want that when the switch is on, there would be a transition to a new activity from the click, otherwise there would be a transition to another activity, but with different parameters. I don't understand what to write in isChecked?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Neonoviiwolf, 2016-12-17
@bychok300

you wrote the switch in the wrong place, transfer it to ClickMe
isChecked returns true/false

if (swi.isChecked){//просто обращаемся к самому свичу и спрашиваем "включен ли он"
//тут пишем интент если выбрано
 }
                else { 
//тут пишем интент, если не выбрано
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question