How to enable the toggle button and enable it if I typed in two text files and a username

I wrote code to check if the username and password are for the administrator or not ... If so, redirect to administrator activity (Activity_2), other wise ones redirect the user to another activity (ac) ... but my code has something then it’s not, because the status of the toggle button has not changed as I want: \

enter image description here

this is my code for main activity:

package com.example.task_7; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.ToggleButton; public class MainActivity extends Activity { Button b1; ToggleButton b2; EditText t1,t2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button) findViewById(R.id.login); b2=(ToggleButton) findViewById(R.id.isAdmin); t1=(EditText) findViewById(R.id.userName); t2=(EditText) findViewById(R.id.password); b2.setChecked(false); b2.setEnabled(false); if(t1.getText().toString().equals("e") && t2.getText().toString().equals("123") ){ b2.setEnabled(true); b2.setText("ON"); } b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent i; if(b2.isChecked()) { i = new Intent(getApplicationContext(), Activity_2.class); i.putExtra("UserName", t1.getText().toString()); i.putExtra("Password", t2.getText().toString()); startActivity(i); } else { i = new Intent(getApplicationContext(), ac.class); i.putExtra("UserName", t1.getText().toString()); startActivity(i); } } }); } } 
+4
source share
3 answers

EDIT You need to add setChecked(true) inside afterTextChanged , or your code will never work because b2.isChecked() will always be false .

This is your problem: the onCreate function onCreate called only when an activity is created.

When the action is created, all the texts are empty, so checking that the TextViews is filled with the username and password of the administrator makes no sense. You must check them every time the user presses a key. You can achieve this by adding addTextChangedListener() to TextViews, so the function inside the listener is called every time the text changes:

 b1=(Button) findViewById(R.id.login); b2=(ToggleButton) findViewById(R.id.isAdmin); t1=(EditText) findViewById(R.id.userName); t2=(EditText) findViewById(R.id.password); TextWatcher listener = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { if(t1.getText().toString().equals("e") && t2.getText().toString().equals("123")){ b2.setChecked(true); b2.setEnabled(true); b2.setText("ON"); } } }; t1.addTextChangedListener(listener); t2.addTextChangedListener(listener); 
0
source

You put this check in the onCreate() method so that it onCreate() only once, at the beginning. You need to create a TextWatcher for EditTexts :

 TextWatcher tw = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void afterTextChanged(final Editable s) { if(t1.getText().toString().equals("e") && t2.getText().toString().equals("123") ){ b2.setEnabled(true); b2.setText("ON"); } } }); t1.addTextChangedListener(tw); t2.addTextChangedListener(tw); 

Thus, every time someone changes the text in one of these EditText s, the condition is checked.

Of course, you can think of something more effective, for example, creating an accept button, and when you click, the condition is checked. But it is up to you.

+2
source

You verify the username and password of the administrator in oncreate, which is called upon creation. To verify a user has entered a username and password, enter this code inside ontextchanged from the edit texts.

 if(t1.getText().toString().equals("e") && t2.getText().toString().equals("123") ){ b2.setEnabled(true); b2.setText("ON"); } 
-1
source

Source: https://habr.com/ru/post/1488235/


All Articles