18

I have an edittext and a button in my layout and in my code I'm setting keyListener of the edittext as null

    editText.setKeyListener(null);

so that I cannot type into my edittext. Now on my button click I should be able to type into my ediitext. How can I do that. It's a simple problem, but I'm not able to find any solution. Any help would be much appreciated.

prolink007
  • 33,872
  • 24
  • 117
  • 185
SKT
  • 1,821
  • 1
  • 20
  • 32

3 Answers3

25

I'm probably late now but, this is the way I do it:

public class MyActivity extends Activity
{
    private KeyListener listener;
    private EditText editText;

    public void onCreate(...)
    {
        editText = ... // Get EditText from somewhere
        listener = editText.getKeyListener(); // Save the default KeyListener!!!
        editText.setKeyListener(null); // Disable input
    }

    // When you click your button, restore the default KeyListener
    public void buttonClickHandler(...)
    {
        editText.setKeyListener(listener);
    }
}

Basically, you first save the EditText's default KeyListener before you call setKeyListener(null). Then, when you click your button, you call setKeyListener again, passing the default listener you previously saved.

sfmirtalebi
  • 370
  • 7
  • 16
rainai
  • 468
  • 4
  • 8
7

You can use this :

// When you click your button, restore the default KeyListener
public void buttonClickHandler(...)
{
    editText.setKeyListener(new EditText(getApplicationContext()).getKeyListener());
}
2

its bug in android See here Bugs.

But in xml file you can do it.Using android:editable="false"

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:editable="false" <<<<<<<
</EditText>
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • Sorry if we do like that we will still be able to type into the edittext in disabled mode also. I cannot accept that as an answer though it may give user a feeling that he may not be able to type into edittext – SKT Jun 07 '12 at 13:52
  • What I want to do is through code only. Because when I tap that button again. I want to make the property of the edittext as it was before(Could not type into it) So I cannot go for xml... But I will check if there is any property like editable in java code... Mean while if you find an answer please help me by posting it...Thankyou – SKT Jun 07 '12 at 14:46