简体   繁体   中英

How do I conditionally enable a TextView based on another TextView's contents?

I'm new to Android Studio, and programming an application.

I'm experimenting with it, and I want to know how you can enable (for example) Textview2 if Textview1 contains something.

I tried this code but it didn't work:

TextView2.setEnabled(false);

    if (TextView1.length() > 0) {
        TextView2.setEnabled(true);
    }

Thanks in advance!

Welcome to SO, You can usecontains method for check if your string has specific text.

if (TextView1.getText().toString().contains("yourText")) {
    TextView2.setEnabled(true);
}

for checking if Textview1 has text. you can use method for checking:

//usage: 
if(TextView1.getText().toString() != null || TextView1.getText().toString().length() > 0) //then do what you want. 

be aware of null pointer exception by checking textview have a text.

I got the answer to my Question!

i did it like this:

if (TextView1.getText().toString().isEmpty()) {

//Do what you want here

}

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