How the Android edittext control prevents input content

Source: Internet
Author: User

Problem?

How does the Android EditText control prohibit entering content inside?


Modified version of the workaround:

EditText EditText = (EditText) Findviewbyid (R.ID.EDITTEXT1);

Edittext.setkeylistener (NULL);


It may be a bit strange to see this question, EditText's function is not to write to the above content?

Furthermore, if you really want to prevent text from being entered, add android:focusable= "false" to the layout file,

or use Edittext.setfocusable (FALSE) in your code, OK?

Project requirements are like this, if the edittext above has been settext () content, you need to prohibit the input, to prevent it from being modified.

If no content is displayed, the EditText is set to an input state.


The test verifies that the effect of the Setfocusable method is only valid for the first time use, that is, if the layout file is set:

Android:focusable= "False" even if you set this control property in code: Edittext.setfocusable (True), and it cannot be edited.

The setfocusable scheme is not feasible. After exploring the feasible solution.

Use Edittext.setinputtype (inputtype.type_null) to disable the phone's soft keyboard.

Edittext.setinputtype (Inputtype.type_class_text); To turn on the soft keyboard.

The application is enabled by default.

Special Note: This method can only prohibit the soft keyboard, if the phone comes with a hard keyboard, this scheme is invalid.


Test demo included:

public class Edittexttest extends Activity

{

/** test EditText forbid input function demo */

EditText EditText;

Boolean flag = true;


public void OnCreate (Bundle savedinstancestate)

{

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

EditText = (editText) Findviewbyid (R.ID.EDITTEXT1);

Button btn = (button) Findviewbyid (R.id.button1);

Btn.setonclicklistener (New Onclicklistener ()

{

public void OnClick (View v)

{

if (flag==true)

{

System.out.println ("Open soft keyboard");

Edittext.setinputtype (Inputtype.type_class_text);

Flag = false;

}else

{

System.out.println ("No soft keyboard");

Edittext.setinputtype (Inputtype.type_null);

Flag = true;

}

}

});

}

}


This article is from the "No Water Fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1708753

How the Android edittext control prevents input content

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.