简体   繁体   中英

Button not disabling if text missing in edittext

Could anyone tell me why my button isn't disabled when the text in the edittext is empty? I've tried to do this so many ways but it never works! This is the simplified code I have at the minute.

Code:

public class MapStartActivity extends FragmentActivity {

    EditText mapName;
    Button NextPageStart;


    private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            checkFieldsForEmptyValues();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    private void checkFieldsForEmptyValues(){
        String s1 = mapName.getText().toString();

        if (s1.trim().isEmpty()) {
            NextPageStart.setEnabled(true);
        } else {
            NextPageStart.setEnabled(false);
            NextPageStart.setAlpha(.5f);
        }

    }

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

        NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
        mapName = (EditText) findViewById(R.id.MapNameText);


        //Click Listener for button
        NextPageStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mapName.addTextChangedListener(textWatcher);

            }
        });
    }
}

Add text watcher outside click listener like this

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

        NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
        mapName = (EditText) findViewById(R.id.MapNameText);

        // To disable the button intially
        NextPageStart.setEnabled(false);

        mapName.addTextChangedListener(textWatcher);

        //Click Listener for button
        NextPageStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // You can do some click action here

            }
        });
    }

In your code change these below lines only

 @Override
    public void afterTextChanged(Editable s) {
        String textFromEditText = mapName.getText();
        if(TextUtils.isEmpty(textFromEditText)){
            NextPageStart.setEnabled(false);
         } else{
             NextPageStart.setEnabled(true);
         }
    }

I don't know why it's not working not for you, but I guess it should work fine. but you should learn about name convention first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM