Edittext中的Edittext可编辑且不可编辑

时间:2017-01-18 11:08:19

标签: android android-edittext

代码

 edtAddress = (EditText) findViewById(R.id.edtAddress);
 edtAddress.setTag(edtAddress.getKeyListener());
 edtAddress.setKeyListener(null);
 imgEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            edtAddress.setKeyListener((KeyListener) edtAddress.getTag());
        }
    });

嗨,我在打开应用程序时遇到问题,编辑文本无法编辑,但是当点击编辑按钮时,编辑文本就可以编辑了。

我已经完成了这个代码,但问题是,当我点击编辑按钮然后编辑文本(edtAddress)是可编辑的,但是,当键入任何字符时,编辑文本没有得到输入字符和应用程序崩溃。

那里有什么问题,建议我

错误

 E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: com.hit.fyl_owner, PID: 3128
                                                             java.lang.IndexOutOfBoundsException
                                                                 at android.graphics.Canvas.drawText(Canvas.java:1696)
                                                                 at android.text.Layout.drawText(Layout.java:410)
                                                                 at android.widget.Editor.drawHardwareAccelerated(Editor.java:1585)
                                                                 at android.widget.Editor.onDraw(Editor.java:1507)
                                                                 at android.widget.TextView.onDraw(TextView.java:5715)
                                                                 at android.view.View.draw(View.java:16178)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:15174)
                                                                 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
                                                                 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:15134)
                                                                 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
                                                                 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:15134)
                                                                 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
                                                                 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:15134)
                                                                 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
                                                                 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:15134)
                                                                 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
                                                                 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:15134)
                                                                 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
                                                                 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:15134)
                                                                 at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
                                                                 at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:15134)
                                                                 at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
                                                                 at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
                                                                 at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
                                                                 at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
                                                                 at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
                                                                 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
                                                                 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                 at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

5 个答案:

答案 0 :(得分:1)

尝试使用此代码,

edtAddress = (EditText) findViewById(R.id.edtAddress);
edtAddress.setEnabled(false);
imgEdit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) 
    {
        edtAddress.setEnabled(true);
        edtAddress.requestFocus();
    }
});

答案 1 :(得分:1)

您可以设置edittext disable。

edtOrganization.setFocusable(false);

现在您可以设置edittext enable。

edtFirst.setFocusable(true);
edtFirst.setFocusableInTouchMode(true);

答案 2 :(得分:0)

您可以添加“requestFocus”,它会聚焦EditText

     <EditText...>
        <requestFocus />
     </EditText>

答案 3 :(得分:0)

将其添加到EditText xml文件中:

<EditText ...
        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false">
</EditText>

答案 4 :(得分:0)

我添加了两个属性 android:singleLine="true" android:imeOptions="actionDone"

,一切开始正常运行。好吧,我仍然不了解原因,可能会对其进行深入研究,但希望这会有所帮助。