Edittext cannot be edited using code in Android

Source: Internet
Author: User

It is easy to disable edittext editing in XML files. You only need to set the value of Android: editable to false.CodeBut there is no seteditable method;

You can only use the following code to disable edittext editing in code:

Edittext. setfilters (New inputfilter [] {New inputfilter () {<br/> @ override <br/> Public charsequence filter (charsequence source, int start, <br/> int end, spanned DEST, int dstart, int dend) {<br/> return source. length () <1? DeST. Subsequence (dstart, dend): ""; <br/>}< br/> });

The following is an example:

Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. text. inputfilter; <br/> Import android. text. spanned; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. edittext; </P> <p>/** <br/> * class which shows how to lock and unlock edittext component <br/> * @ author faynasoft labs <br/> */<br/> public class main extends Activity {<Br/> private edittext; <br/> private Boolean value = false; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> edittext = (edittext) findviewbyid (R. id. textid); <br/> edittext. settext ("edittext component"); <br/> button B = (button) findviewbyid (R. id. btnid); <br/> B. settext ("Lock /Unlock "); <br/> B. setonclicklistener (new view. onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> If (value) {<br/> value = false; <br/>}else {<br/> value = true; <br/>}< br/> lockunlock (value ); <br/>}< br/> }); <br/>}</P> <p>/** <br/> * method which locks and unlocks edittext component <br/> * @ Param value our Boolean value which using in or if operator <br/> */ <Br/> private void lockunlock (Boolean value) {<br/> If (value) {<br/> edittext. setfilters (New inputfilter [] {New inputfilter () {<br/> @ override <br/> Public charsequence filter (charsequence source, int start, <br/> int end, spanned DEST, int dstart, int dend) {<br/> return source. length () <1? DeST. subsequence (dstart, dend) <br/>: ""; <br/>}< br/>}); <br/>} else {<br/> edittext. setfilters (New inputfilter [] {New inputfilter () {<br/> @ override <br/> Public charsequence filter (charsequence source, int start, <br/> int end, spanned DEST, int dstart, int dend) {<br/> return NULL; <br/>}< br/> }}); <br/>}< br/>}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.