Edit Text key listener

旧街凉风 提交于 2019-11-29 17:17:32

问题


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.


回答1:


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.




回答2:


You can use this :

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



回答3:


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>


来源:https://stackoverflow.com/questions/10933056/edit-text-key-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!