Answer the question
In order to leave comments, you need to log in
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);
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question