简体   繁体   中英

Getting data from edittext when switchbox is enabled in Android using Java

So, I got problem to get data from edittext when I cheked the switch box first I set the visibility of edittext is gone, and then when the switchbox is enabled/cheked, the edittext is shown, and then if edittext is not null or empty, it will be set the value of string komentar into what I put in the edittext, but I can't get the data from edittext if write the code like this:

String komentar="";
    Switch switch_komentar = (Switch) findViewById(R.id.switch_komentar);
            edittext_komentar = (EditText) findViewById(R.id.text_komentar);
            switch_komentar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
                    if(isChecked){
                        edittext_komentar.setVisibility(View.VISIBLE);
                        if(edittext_komentar.isShown() && !edittext_komentar.equals("")){
    
                            komentar=edittext_komentar.getText().toString();
                        }
    
                    }else {
                        edittext_komentar.setVisibility(View.GONE);
                    }
                }
            });

But, when I try to enabled the switchbox 2 times, the data from the edittext I can get it, but when I'm change it again, the value is not changed, but when I'm try to enbaled for 2 times again, I can get it.

this problem is solved, so im try to get the value of edittext not inside the shitchbox, im try to get the value outside the switchbox listener, thankyou

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